-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththis.nix
217 lines (181 loc) · 9.28 KB
/
this.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Attribute set of NixOS configurations found in each directory
{ inputs, caches ? [], ... }: let
inherit (lib) ls mkAttrs mkUsers mkList lsUsers configurationNameFromPath profileNameFromPath userNameFromPath;
# Personal lib
lib = {
# List directories and files that can be imported by nix
# ls ./modules;
# ls { path = ./modules; dirsWith = [ "default.nix" "home.nix" ]; filesExcept = [ "default.nix" ]; asPath = true; };
ls = x: ( let
inherit (builtins) attrNames concatMap elem filter isAttrs isPath pathExists readDir;
inherit (inputs.nixpkgs.lib) filterAttrs hasPrefix hasSuffix removeSuffix unique;
# Return list of directory names (with default.nix) inside path
dirNames = path: dirsWith: asPath: let
dirs = attrNames (filterAttrs (n: v: v == "directory") (readDir path));
isVisible = (name: (!hasPrefix "." name));
dirsWithFiles = (dirs: concatMap (dir: concatMap (file: ["${dir}/${file}"] ) dirsWith) dirs);
isValid = dirFile: pathExists "${path}/${dirFile}";
format = paths: map (dirFile: (if (asPath == true) then path + "/${dirFile}" else dirOf dirFile)) paths;
in format (filter isValid (dirsWithFiles (filter isVisible dirs)));
# Return list of filenames (ending in .nix) inside path
fileNames = path: filesExcept: asPath: let
files = attrNames (filterAttrs (n: v: v == "regular") (readDir path));
isVisible = (name: (!hasPrefix "." name));
isNix = (name: (hasSuffix ".nix" name));
isAllowed = (name: !elem name filesExcept);
format = paths: map (file: (if (asPath == true) then path + "/${file}" else file)) paths;
in format (filter isAllowed (filter isNix (filter isVisible files)));
# Shortcut to pass path directly with default options
fromPath = path: fromAttrs { inherit path; };
# Return list of directory/file names if asPath is false, otherwise list of absolute paths
fromAttrs = { path, dirsWith ? [ "default.nix" ], filesExcept ? [ "flake.nix" "default.nix" "configuration.nix" "nixos.nix" "home.nix" "this.nix" ], asPath ? true }: unique
(if ! pathExists path then [] else # If path doesn't exist, return an empty list
(if hasSuffix ".nix" path then [ path ] else # If path is a nix file, return that path in a list
(if dirsWith == false then [] else (dirNames path dirsWith asPath)) ++ # No subdirs if dirsWith is false,
(if filesExcept == false then [] else (fileNames path filesExcept asPath)) # No files if filesExcept is false
)
);
in
if (isPath x) then (fromPath x)
else if (isAttrs x) then (fromAttrs x)
else []
);
# Create list from path or list
mkList = x: ( let
inherit (builtins) isPath isList pathExists;
inherit (inputs.nixpkgs.lib) removeSuffix;
# Create list from files and subdirectories of path
fromPath = path: if ! pathExists path then [] else map
( filename: removeSuffix ".nix" filename )
( ls { inherit path; asPath = false; } );
# Create list from list of values
fromList = list: map
( filename: removeSuffix ".nix" filename )
( list );
in
if (isPath x) then (fromPath x)
else if (isList x) then (fromList x)
else []
);
# Create attrs from list, attr names, or path
mkAttrs = x: fn: ( let
inherit (builtins) attrNames listToAttrs isAttrs isPath isList pathExists;
inherit (inputs.nixpkgs.lib) removeSuffix;
# Create attribute set from files and subdirectories of path
fromPath = path: listToAttrs ( map
( name: { name = (removeSuffix ".nix" name); value = (fn name); })
( ls { inherit path; asPath = false; } )
);
# Create attribute set list of values
fromList = list: listToAttrs ( map
( name: { name = (removeSuffix ".nix" name); value = (fn name); })
( list )
);
# Do the same as above using the attrNames
fromAttrs = attrs: fromList (attrNames attrs);
in
if (isPath x) then (fromPath x)
else if (isList x) then (fromList x)
else if (isAttrs x) then (fromAttrs x)
else {}
);
# Convert a user path to a user name
userNameFromPath = path: let
inherit (builtins) toString;
inherit (inputs.nixpkgs.lib) removeSuffix;
in baseNameOf( removeSuffix ".nix" (removeSuffix "/home.nix" (toString path) ) );
# Convert a configuration path to a configuration name
configurationNameFromPath = path: let
inherit (builtins) toString;
inherit (inputs.nixpkgs.lib) removeSuffix;
in baseNameOf( removeSuffix "/this.nix" (removeSuffix "/configuration.nix" (removeSuffix "/default.nix" (toString path) ) ) );
# Like mkAttrs but only includes paths to configuration directories with this.nix
mkConfigurations = fn: builtins.listToAttrs ( map
( path: { name = configurationNameFromPath path; value = (fn path); } )
( ls { path = ./configurations/systems; asPath = true; dirsWith = [ "this.nix" ]; } )
);
# Like mkAttrs but only includes paths to user nix files or user directories with home.nix
mkUsers = hostName: fn: builtins.listToAttrs ( map
( path: { name = userNameFromPath path; value = (fn path); } )
( ls { path = ./configurations/systems/${hostName}/users; asPath = true; dirsWith = [ "home.nix" ]; } )
);
# List of users for a particular nixos configuration
lsUsers = this: mkList( ls {
path = ./configurations/systems/${this.hostName}/users;
asPath = false; dirsWith = [ "home.nix" ];
});
# List of users with a public key in the secrets directory
lsAdmins = this: let
inherit (this.inputs.nixpkgs.lib) attrNames intersectLists remove;
in intersectLists ( lsUsers this ) (
remove "all" ( attrNames (import ./secrets/keys).users )
);
# NixOS modules imported in each configuration
mkModules = this: let
inherit (this) hostName;
inherit (this.inputs) nix-index-database;
inherit (this.inputs.nixpkgs.lib) hasPrefix mkDefault partition;
# Prepare cache module from list of pairs
nix-cache = let
pair = (partition (value: (hasPrefix "https://" value)) caches);
urls = pair.right; keys = pair.wrong;
in [( { ... }: {
nix.settings.substituters = urls;
nix.settings.trusted-substituters = urls;
nix.settings.trusted-public-keys = keys;
}) ];
# Prepare nix-index module with weekly updated database and comma integration
nix-index = let config = { ... }: {
programs.nix-index-database.comma.enable = mkDefault true;
programs.nix-index.enableBashIntegration = mkDefault false;
programs.nix-index.enableZshIntegration = mkDefault false;
programs.nix-index.enableFishIntegration = mkDefault false;
programs.command-not-found.enable = mkDefault false;
}; in {
nixos = [ nix-index-database.nixosModules.nix-index config ];
home = [ nix-index-database.hmModules.nix-index config ];
};
# Include shared modules followed by dir-specific modules
in
# Home Manager modules are organized under each user's name
mkUsers hostName (
userPath: let userName = userNameFromPath userPath; in
ls { path = ./modules; dirsWith = [ "home.nix" ]; } ++ # home-manager modules
ls ./configurations/users/all/home.nix ++ # shared home-manager configuration for all users
ls ./configurations/users/${userName}.nix ++ # shared home-manager configuration for one user
ls ./configurations/users/${userName}/home.nix ++
ls ./configurations/systems/${hostName}/users/${userName}.nix ++ # specific home-manager configuration for one user
ls ./configurations/systems/${hostName}/users/${userName}/home.nix ++
[ ./secrets ] ++ nix-cache ++ nix-index.home # secrets, keys, cache and index
# NixOS modules are organization under "root"
) // {
root =
ls { path = ./modules; dirsWith = [ "nixos.nix" ]; } ++ # nixos modules
ls ./configurations/systems/all/configuration.nix ++ # shared nixos configuration for all systems
ls ./configurations/systems/${hostName}/configuration.nix ++ # specific nixos configuration for one system
[ ./secrets ] ++ nix-cache ++ nix-index.nixos # secrets, keys, cache and index
;
};
profileNameFromPath = path: let
inherit (builtins) toString;
inherit (inputs.nixpkgs.lib) removeSuffix;
in baseNameOf( removeSuffix "/nixos.nix" (removeSuffix "/home.nix" (toString path) ) );
mkProfiles = nixosOrHome: builtins.listToAttrs ( map
( path: { name = profileNameFromPath path; value = (path); } )
( ls { path = ./configurations/profiles; asPath = true; dirsWith = [ "${nixosOrHome}.nix" ]; } )
);
};
in {
# inputs + lib accessible from this
inherit inputs lib;
# Default values, overridden in configuration/*/default.nix
hostName = "nixos";
domain = "";
users = []; # without users, only root exists
admins = []; # allow sudo/ssh powers users with keys
modules = {}; # includes for nixos and home-manager
profiles = {}; # includes for nixos and home-manager
system = "x86_64-linux";
stable = true;
config = {};
}