summaryrefslogtreecommitdiffstats
path: root/src/detection/packages/packages_nix.c
blob: d1cc2647e3c37ffa662e02489c8dd3925cc5196f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "packages.h"
#include "common/io.h"
#include "common/processing.h"
#include "common/strutil.h"

static bool isValidNixPkg(FFstrbuf* pkg) {
    if (!ffPathExists(pkg->chars, FF_PATHTYPE_DIRECTORY)) {
        return false;
    }

    ffStrbufSubstrAfterLastC(pkg, '/');
    if (
        ffStrbufStartsWithS(pkg, "nixos-system-nixos-") ||
        ffStrbufEndsWithS(pkg, "-doc") ||
        ffStrbufEndsWithS(pkg, "-man") ||
        ffStrbufEndsWithS(pkg, "-info") ||
        ffStrbufEndsWithS(pkg, "-dev") ||
        ffStrbufEndsWithS(pkg, "-bin")) {
        return false;
    }

    enum { START,
        DIGIT,
        DOT,
        MATCH } state = START;

    for (uint32_t i = 0; i < pkg->length; i++) {
        char c = pkg->chars[i];
        switch (state) {
            case START:
                if (ffCharIsDigit(c)) {
                    state = DIGIT;
                }
                break;
            case DIGIT:
                if (ffCharIsDigit(c)) {
                    continue;
                }
                if (c == '.') {
                    state = DOT;
                } else {
                    state = START;
                }
                break;
            case DOT:
                if (ffCharIsDigit(c)) {
                    state = MATCH;
                } else {
                    state = START;
                }
                break;
            case MATCH:
                break;
        }
    }

    return state == MATCH;
}

static bool checkNixCache(FFstrbuf* cacheDir, FFstrbuf* hash, uint32_t* count) {
    if (!ffPathExists(cacheDir->chars, FF_PATHTYPE_FILE)) {
        return false;
    }

    FF_STRBUF_AUTO_DESTROY cacheContent = ffStrbufCreate();
    if (!ffReadFileBuffer(cacheDir->chars, &cacheContent)) {
        return false;
    }

    // Format: <hash>\n<count>
    uint32_t split = ffStrbufFirstIndexC(&cacheContent, '\n');
    if (split == cacheContent.length) {
        return false;
    }

    ffStrbufSetNS(hash, split, cacheContent.chars);
    *count = (uint32_t) atoi(cacheContent.chars + split + 1);

    return true;
}

static bool writeNixCache(FFstrbuf* cacheDir, FFstrbuf* hash, uint32_t count) {
    FF_STRBUF_AUTO_DESTROY cacheContent = ffStrbufCreateCopy(hash);
    ffStrbufAppendF(&cacheContent, "\n%u", count);
    return ffWriteFileBuffer(cacheDir->chars, &cacheContent);
}

static uint32_t getNixPackagesImpl(char* path) {
    // Nix detection is kinda slow, so we only do it if the dir exists
    if (!ffPathExists(path, FF_PATHTYPE_DIRECTORY)) {
        return 0;
    }

    FF_STRBUF_AUTO_DESTROY cacheDir = ffStrbufCreateCopy(&instance.state.platform.cacheDir);
    ffStrbufEnsureEndsWithC(&cacheDir, '/');
    ffStrbufAppendS(&cacheDir, "fastfetch/packages/nix");
    ffStrbufAppendS(&cacheDir, path);

    // Check the hash first to determine if we need to recompute the count
    FF_STRBUF_AUTO_DESTROY hash = ffStrbufCreateA(64);
    FF_STRBUF_AUTO_DESTROY cacheHash = ffStrbufCreateA(64);
    uint32_t count = 0;

    ffProcessAppendStdOut(&hash, (char* const[]) { "nix-store", "--query", "--hash", path, NULL });

    if (checkNixCache(&cacheDir, &cacheHash, &count) && ffStrbufEqual(&hash, &cacheHash)) {
        return count;
    }

    // Cache is invalid, recompute the count
    count = 0;

    // Implementation based on bash script from here:
    // https://github.com/fastfetch-cli/fastfetch/issues/195#issuecomment-1191748222

    FF_STRBUF_AUTO_DESTROY output = ffStrbufCreateA(1024);

    ffProcessAppendStdOut(&output, (char* const[]) { "nix-store", "--query", "--requisites", path, NULL });

    uint32_t lineLength = 0;
    for (uint32_t i = 0; i < output.length; i++) {
        if (output.chars[i] != '\n') {
            lineLength++;
            continue;
        }

        output.chars[i] = '\0';
        FFstrbuf line = {
            .allocated = 0,
            .length = lineLength,
            .chars = output.chars + i - lineLength
        };
        if (isValidNixPkg(&line)) {
            count++;
        }
        lineLength = 0;
    }

    writeNixCache(&cacheDir, &hash, count);
    return count;
}

uint32_t ffPackagesGetNix(FFstrbuf* baseDir, const char* dirname) {
    uint32_t baseDirLength = baseDir->length;
    ffStrbufAppendS(baseDir, dirname);
    uint32_t num_elements = getNixPackagesImpl(baseDir->chars);
    ffStrbufSubstrBefore(baseDir, baseDirLength);
    return num_elements;
}