1
1
#include "packages.h"
2
2
#include "common/io/io.h"
3
3
#include "common/parsing.h"
4
- #include "common/processing.h"
5
4
#include "common/properties.h"
6
5
#include "common/settings.h"
7
6
#include "detection/os/os.h"
@@ -124,157 +123,6 @@ static uint32_t countFilesRecursive(FFstrbuf* baseDir, const char* dirname, cons
124
123
return sum ;
125
124
}
126
125
127
- static bool isValidNixPkg (FFstrbuf * pkg )
128
- {
129
- if (!ffPathExists (pkg -> chars , FF_PATHTYPE_DIRECTORY ))
130
- return false;
131
-
132
- ffStrbufSubstrAfterLastC (pkg , '/' );
133
- if (
134
- ffStrbufStartsWithS (pkg , "nixos-system-nixos-" ) ||
135
- ffStrbufEndsWithS (pkg , "-doc" ) ||
136
- ffStrbufEndsWithS (pkg , "-man" ) ||
137
- ffStrbufEndsWithS (pkg , "-info" ) ||
138
- ffStrbufEndsWithS (pkg , "-dev" ) ||
139
- ffStrbufEndsWithS (pkg , "-bin" )
140
- ) return false;
141
-
142
- enum { START , DIGIT , DOT , MATCH } state = START ;
143
-
144
- for (uint32_t i = 0 ; i < pkg -> length ; i ++ )
145
- {
146
- char c = pkg -> chars [i ];
147
- switch (state )
148
- {
149
- case START :
150
- if (ffCharIsDigit (c ))
151
- state = DIGIT ;
152
- break ;
153
- case DIGIT :
154
- if (ffCharIsDigit (c ))
155
- continue ;
156
- if (c == '.' )
157
- state = DOT ;
158
- else
159
- state = START ;
160
- break ;
161
- case DOT :
162
- if (ffCharIsDigit (c ))
163
- state = MATCH ;
164
- else
165
- state = START ;
166
- break ;
167
- case MATCH :
168
- break ;
169
- }
170
- }
171
-
172
- return state == MATCH ;
173
- }
174
-
175
- static bool checkNixCache (FFstrbuf * cacheDir , FFstrbuf * hash , uint32_t * count )
176
- {
177
- if (!ffPathExists (cacheDir -> chars , FF_PATHTYPE_FILE ))
178
- return false;
179
-
180
- FF_STRBUF_AUTO_DESTROY cacheContent = ffStrbufCreate ();
181
- if (!ffReadFileBuffer (cacheDir -> chars , & cacheContent ))
182
- return false;
183
-
184
- // Format: <hash>\n<count>
185
- uint32_t split = ffStrbufFirstIndexC (& cacheContent , '\n' );
186
- if (split == cacheContent .length )
187
- return false;
188
-
189
- ffStrbufSetNS (hash , split , cacheContent .chars );
190
- * count = (uint32_t )atoi (cacheContent .chars + split + 1 );
191
-
192
- return true;
193
- }
194
-
195
- static bool writeNixCache (FFstrbuf * cacheDir , FFstrbuf * hash , uint32_t count )
196
- {
197
- FF_STRBUF_AUTO_DESTROY cacheContent = ffStrbufCreateCopy (hash );
198
- ffStrbufAppendF (& cacheContent , "\n%u" , count );
199
- return ffWriteFileBuffer (cacheDir -> chars , & cacheContent );
200
- }
201
-
202
- static uint32_t getNixPackagesImpl (char * path )
203
- {
204
- //Nix detection is kinda slow, so we only do it if the dir exists
205
- if (!ffPathExists (path , FF_PATHTYPE_DIRECTORY ))
206
- return 0 ;
207
-
208
- FF_STRBUF_AUTO_DESTROY cacheDir = ffStrbufCreateCopy (& instance .state .platform .cacheDir );
209
- ffStrbufEnsureEndsWithC (& cacheDir , '/' );
210
- ffStrbufAppendS (& cacheDir , "fastfetch/packages/nix" );
211
- ffStrbufAppendS (& cacheDir , path );
212
-
213
- //Check the hash first to determine if we need to recompute the count
214
- FF_STRBUF_AUTO_DESTROY hash = ffStrbufCreateA (64 );
215
- FF_STRBUF_AUTO_DESTROY cacheHash = ffStrbufCreateA (64 );
216
- uint32_t count = 0 ;
217
-
218
- ffProcessAppendStdOut (& hash , (char * const []) {
219
- "nix-store" ,
220
- "--query" ,
221
- "--hash" ,
222
- path ,
223
- NULL
224
- });
225
-
226
- if (checkNixCache (& cacheDir , & cacheHash , & count ) && ffStrbufEqual (& hash , & cacheHash ))
227
- return count ;
228
-
229
- //Cache is invalid, recompute the count
230
- count = 0 ;
231
-
232
- //Implementation based on bash script from here:
233
- //https://github.com/fastfetch-cli/fastfetch/issues/195#issuecomment-1191748222
234
-
235
- FF_STRBUF_AUTO_DESTROY output = ffStrbufCreateA (1024 );
236
-
237
- ffProcessAppendStdOut (& output , (char * const []) {
238
- "nix-store" ,
239
- "--query" ,
240
- "--requisites" ,
241
- path ,
242
- NULL
243
- });
244
-
245
- uint32_t lineLength = 0 ;
246
- for (uint32_t i = 0 ; i < output .length ; i ++ )
247
- {
248
- if (output .chars [i ] != '\n' )
249
- {
250
- lineLength ++ ;
251
- continue ;
252
- }
253
-
254
- output .chars [i ] = '\0' ;
255
- FFstrbuf line = {
256
- .allocated = 0 ,
257
- .length = lineLength ,
258
- .chars = output .chars + i - lineLength
259
- };
260
- if (isValidNixPkg (& line ))
261
- count ++ ;
262
- lineLength = 0 ;
263
- }
264
-
265
- writeNixCache (& cacheDir , & hash , count );
266
- return count ;
267
- }
268
-
269
- static uint32_t getNixPackages (FFstrbuf * baseDir , const char * dirname )
270
- {
271
- uint32_t baseDirLength = baseDir -> length ;
272
- ffStrbufAppendS (baseDir , dirname );
273
- uint32_t num_elements = getNixPackagesImpl (baseDir -> chars );
274
- ffStrbufSubstrBefore (baseDir , baseDirLength );
275
- return num_elements ;
276
- }
277
-
278
126
static uint32_t getXBPSImpl (FFstrbuf * baseDir )
279
127
{
280
128
DIR * dir = opendir (baseDir -> chars );
@@ -572,8 +420,8 @@ static void getPackageCounts(FFstrbuf* baseDir, FFPackagesResult* packageCounts,
572
420
if (!(options -> disabled & FF_PACKAGES_FLAG_FLATPAK_BIT )) packageCounts -> flatpakSystem += getFlatpakPackages (baseDir , "/var/lib" );
573
421
if (!(options -> disabled & FF_PACKAGES_FLAG_NIX_BIT ))
574
422
{
575
- packageCounts -> nixDefault += getNixPackages (baseDir , "/nix/var/nix/profiles/default" );
576
- packageCounts -> nixSystem += getNixPackages (baseDir , "/run/current-system" );
423
+ packageCounts -> nixDefault += ffPackagesGetNix (baseDir , "/nix/var/nix/profiles/default" );
424
+ packageCounts -> nixSystem += ffPackagesGetNix (baseDir , "/run/current-system" );
577
425
}
578
426
if (!(options -> disabled & FF_PACKAGES_FLAG_PACMAN_BIT )) packageCounts -> pacman += getNumElements (baseDir , "/var/lib/pacman/local" , true);
579
427
if (!(options -> disabled & FF_PACKAGES_FLAG_LPKGBUILD_BIT )) packageCounts -> lpkgbuild += getNumElements (baseDir , "/opt/Loc-OS-LPKG/lpkgbuild/remove" , false);
@@ -673,7 +521,7 @@ void ffDetectPackagesImpl(FFPackagesResult* result, FFPackagesOptions* options)
673
521
if (!(options -> disabled & FF_PACKAGES_FLAG_NIX_BIT ))
674
522
{
675
523
// Count packages from $HOME/.nix-profile
676
- result -> nixUser += getNixPackages (& baseDir , ".nix-profile" );
524
+ result -> nixUser += ffPackagesGetNix (& baseDir , ".nix-profile" );
677
525
678
526
// Check in $XDG_STATE_HOME/nix/profile
679
527
FF_STRBUF_AUTO_DESTROY stateHome = ffStrbufCreate ();
@@ -688,11 +536,11 @@ void ffDetectPackagesImpl(FFPackagesResult* result, FFPackagesOptions* options)
688
536
ffStrbufSet (& stateHome , & instance .state .platform .homeDir );
689
537
ffStrbufAppendS (& stateHome , ".local/state/" );
690
538
}
691
- result -> nixUser += getNixPackages (& stateHome , "nix/profile" );
539
+ result -> nixUser += ffPackagesGetNix (& stateHome , "nix/profile" );
692
540
693
541
// Check in /etc/profiles/per-user/$USER
694
542
FF_STRBUF_AUTO_DESTROY userPkgsDir = ffStrbufCreateStatic ("/etc/profiles/per-user/" );
695
- result -> nixUser += getNixPackages (& userPkgsDir , instance .state .platform .userName .chars );
543
+ result -> nixUser += ffPackagesGetNix (& userPkgsDir , instance .state .platform .userName .chars );
696
544
}
697
545
698
546
if (!(options -> disabled & FF_PACKAGES_FLAG_GUIX_BIT ))
0 commit comments