-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyLib.nix
47 lines (43 loc) · 1.46 KB
/
myLib.nix
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
{
lib,
config,
...
}: let
myLib = {
# Get the character at the given index of a string
charAt = i: s: builtins.substring i (i + 1) s;
# Convert a string to a nix path
toPath = s:
if myLib.charAt 0 s == "/"
then /. + s
else if builtins.substring 0 2 s == "./"
then ./. + builtins.substring 1 (builtins.stringLength s) s
else throw "Path must be either absolute (starting with \"/\") or relative (starting with \"./\")";
# Copy other files to store and link them to `/run/current-system/`
createCopyExtraConfigFilesScript = paths: let
newline = ''
'';
makeLinkCommand = path: let
relativePath = builtins.toString path;
in ''
target="''${out}/extra-config-files/$(realpath --canonicalize-missing --relative-base=/etc/nixos "${relativePath}")"
mkdir -p "$(dirname "''$target")"
ln -s '${path}' "''$target"
'';
linkCommands = map makeLinkCommand paths;
in
builtins.concatStringsSep newline linkCommands;
# Check if the current NixOS version is at least the given version
nixosMinVersion = version:
(
builtins.compareVersions
config.system.nixos.release
version
)
>= 0;
# Checks if a given path exists and returns either a list with the path as
# single element or an empyt list if the path does not exist.
importIfExists = path: lib.optional (builtins.pathExists path) path;
};
in
myLib