nixos-configuration/default.nix

44 lines
2.1 KiB
Nix
Raw Normal View History

2024-06-17 07:54:59 +02:00
# if evaluating outside of the store, copy the current directory to the store and import it
# filtering out .gitignore files and .git directories
# if evaluating inside the store, import the outputs.nix file
let
2025-01-13 00:46:20 +01:00
# Ideally this file should not depend on nixpkgs lib itself, but I like the utilities here
lib = (import "${(import ./inputs.nix {}).nixpkgs}/lib");
2024-06-17 07:54:59 +02:00
gitignore = builtins.filter (v:
# ignore comments and empty lines
if !(builtins.isString v) then false
else if !builtins.isNull(builtins.match "^#.*" v) then false
else if !builtins.isNull(builtins.match "^$" v) then false
else true
) (builtins.split "\n" (builtins.readFile ./.gitignore));
# checks if a given path matches a gitignore pattern
# string -> bool
matchesGitIgnore = path: builtins.any (pattern:
let
patternLength = builtins.stringLength pattern;
unsupportedPatternMessage = "matchesGitIgnore: Unsupported pattern: ${pattern}";
in
if pattern == "*" then true
else if pattern == ".*" then true
else if pattern == "*.*" then true
2025-01-13 00:46:20 +01:00
else if builtins.substring 0 2 pattern == "*." then lib.hasSuffix (builtins.substring 0 2 pattern) path
else if lib.hasInfix "*" pattern then abort unsupportedPatternMessage
2024-06-17 07:54:59 +02:00
else if patternLength > 2 && builtins.substring 0 2 pattern == "./" then abort unsupportedPatternMessage
else if patternLength > 1 && builtins.substring 0 1 pattern == "/" then abort unsupportedPatternMessage
2025-01-13 00:46:20 +01:00
else lib.hasInfix pattern path
2024-06-17 07:54:59 +02:00
) gitignore;
currentFilePath = (builtins.unsafeGetAttrPos "any" { any = "any"; }).file;
storePathLength = builtins.stringLength (builtins.toString builtins.storeDir);
evaluatingInStore = (builtins.substring 0 storePathLength currentFilePath) == builtins.storeDir;
selfInStore = builtins.filterSource (path: type:
type != "unknown" && builtins.baseNameOf path != ".git" && !matchesGitIgnore path
) ./.;
in
2024-07-07 08:34:47 +02:00
if !(evaluatingInStore) then { ... }@args: import selfInStore ({ selfPath = selfInStore; } // args )
else { ... }@args: import ./outputs.nix ({ selfPath = selfInStore; } // args)