]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
aubergine: comment typo
[julm/julm-nix.git] / flake.nix
1 {
2 description = "julm's Nix configurations for hosts (NixOS) and homes (home-manager)";
3 /*
4 nixConfig = {
5 extra-substituters = [
6 "https://nix-community.cachix.org"
7 ];
8 extra-trusted-public-keys = [
9 "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
10 ];
11 };
12 */
13
14 inputs = {
15 doom-emacs.flake = false;
16 doom-emacs.url = "github:hlissner/doom-emacs";
17 git-hooks.inputs.nixpkgs.follows = "nixpkgs";
18 git-hooks.url = "github:cachix/git-hooks.nix";
19 home-manager.inputs.nixpkgs.follows = "nixpkgs";
20 #home-manager.url = "github:nix-community/home-manager/master";
21 home-manager.url = "git+file:///home/julm/src/nix/home-manager";
22 nixos-hardware.url = "github:NixOS/nixos-hardware/master";
23 #nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
24 nixpkgs.url = "github:NixOS/nixpkgs/9cb344e96d5b6918e94e1bca2d9f3ea1e9615545";
25 nixpkgs-unstable.url = "github:NixOS/nixpkgs/master";
26 lanzaboote = {
27 url = "github:nix-community/lanzaboote/v0.4.2";
28 inputs.nixpkgs.follows = "nixpkgs";
29 inputs.pre-commit-hooks-nix.follows = "git-hooks";
30 };
31 };
32
33 outputs =
34 inputs:
35 let
36 remoteNixpkgsPatches = import nixpkgs/patches.nix;
37 localNixpkgsPatches = [
38 nixpkgs/patches/openvpn/0001-nixos-netns-init-module-to-manage-network-namespaces.patch
39 nixpkgs/patches/openvpn/0002-nixos-openvpn-add-netns-support.patch
40 nixpkgs/patches/syncoid/0001-nixos-sanoid-add-utilities-useful-to-syncoid.patch
41 nixpkgs/patches/syncoid/0002-nixos-syncoid-use-DynamicUser.patch
42 ];
43 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
44 nixpkgsPath = originPkgs.applyPatches {
45 name = "nixpkgs-patched";
46 src = inputs.nixpkgs.outPath;
47 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
48 postPatch = ''
49 patch=$(printf '%s\n' ${
50 builtins.concatStringsSep " " (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)
51 } |
52 sort | sha256sum | cut -c -7)
53 echo "-patch-$patch" >.version-suffix
54 '';
55 };
56 profile = "/nix/var/nix/profiles/system";
57 inherit (inputs.nixpkgs) lib;
58 overlays =
59 system:
60 import nixpkgs/overlays.nix
61 ++ [
62 ];
63 #nixosSystem = lib.nixosSystem;
64 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
65 forAllSystems =
66 f:
67 lib.genAttrs lib.systems.flakeExposed (
68 system:
69 f rec {
70 inherit system;
71 #pkgs = inputs.nixpkgs.legacyPackages.${system};
72 pkgs = import nixpkgsPath {
73 inherit system;
74 overlays = overlays system;
75 };
76 }
77 );
78 self =
79 with lib.fileset;
80 toSource {
81 root = ./.;
82 fileset =
83 let
84 exts = [
85 "clear"
86 "conf"
87 "cred"
88 "crt"
89 "css"
90 "el"
91 "hs"
92 "json"
93 "lua"
94 "nix"
95 "patch"
96 "pem"
97 "pub"
98 "sh"
99 "theme"
100 "vim"
101 ];
102 in
103 unions [
104 (fileFilter (file: lib.any file.hasExt exts) ./domains)
105 (fileFilter (file: lib.any file.hasExt exts) ./home-manager)
106 (fileFilter (file: lib.any file.hasExt exts) ./homes)
107 (fileFilter (file: lib.any file.hasExt exts) ./hosts)
108 (fileFilter (file: lib.any file.hasExt exts) ./nixos)
109 (fileFilter (file: lib.any file.hasExt exts) ./nixpkgs)
110 (fileFilter (file: lib.any file.hasExt exts) ./users)
111 ./shell.nix
112 ];
113 };
114 in
115 rec {
116 # nix -L build .#hello
117 packages = forAllSystems ({ pkgs, ... }: pkgs);
118
119 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
120 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
121 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
122 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.ruleset
123 nixosConfigurations =
124 lib.genAttrs
125 (builtins.attrNames (
126 lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (self + "/hosts"))
127 ))
128 (
129 hostName:
130 nixosSystem {
131 system = null;
132 specialArgs = {
133 # Required to avoid infinite recursion
134 # when `inputs` is used in `imports`.
135 inherit inputs;
136 };
137 modules = [
138 nixos/default.nix
139 (import (self + "/hosts/${hostName}.nix"))
140 inputs.lanzaboote.nixosModules.lanzaboote
141 inputs.home-manager.nixosModules.home-manager
142 (
143 { config, ... }:
144 let
145 system = config.nixpkgs.hostPlatform.system;
146 pkgs-unstable = import inputs.nixpkgs-unstable.outPath {
147 inherit system;
148 overlays = overlays system;
149 };
150 in
151 {
152 _module.args = {
153 inherit hostName pkgs-unstable;
154 hosts = nixosConfigurations;
155 host = nixosConfigurations.${hostName}._module.args;
156 };
157 nixpkgs.overlays = overlays config.nixpkgs.hostPlatform.system;
158 nixpkgs.config.permittedInsecurePackages = [
159 # Still needed for chatty
160 # See https://github.com/NixOS/nixpkgs/pull/334638#issuecomment-2289025802%3E
161 # and https://gitlab.gnome.org/World/Chatty/-/issues/932
162 "olm-3.2.16"
163 ];
164 nixpkgs.config.allowUnfreePredicate =
165 pkg:
166 builtins.elem (lib.getName pkg) [
167 "anydesk"
168 "canon-cups-ufr2"
169 "cudatoolkit"
170 "geogebra"
171 "hplip"
172 "memtest86-efi"
173 "nvidia-settings"
174 "nvidia-x11"
175 ];
176 home-manager.useGlobalPkgs = true;
177 home-manager.useUserPackages = true;
178 home-manager.verbose = true;
179 #home-manager.force = true;
180 home-manager.backupFileExtension = "old";
181 home-manager.extraSpecialArgs = {
182 inherit hostName inputs pkgs-unstable;
183 };
184 }
185 )
186 ];
187 }
188 );
189
190 # nix -L develop or direnv allow
191 devShells = forAllSystems (
192 { pkgs, system, ... }:
193 {
194 default = pkgs.callPackage (self + "/shell.nix") {
195 inherit
196 pkgs
197 inputs
198 system
199 nixpkgsPath
200 ;
201 inherit (checks.${system}.git-hooks-check) shellHook;
202 };
203 }
204 );
205
206 # nix -L run .#oignon.switch
207 apps = forAllSystems (
208 { pkgs, system, ... }:
209 with builtins;
210 mapAttrs (
211 hostName:
212 { config, ... }:
213 let
214 inherit (config.system) build;
215 scriptApp = scriptName: ps: script: {
216 type = "app";
217 program =
218 (pkgs.writeShellScript "${hostName}-${scriptName}" ''
219 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
220 set -eux
221 ${script}
222 '').outPath;
223 };
224 in
225 {
226 # Example: nix run .#aubergine.switch
227 "switch" = scriptApp "switch" [ ] ''
228 shopt -s globstar
229
230 chmod -R g-rwx,o-rwx **/*.gpg
231 trap 'git reset **/*.gpg' EXIT
232 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
233
234 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
235 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
236
237 nix copy --to "ssh://${config.install.target}?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
238
239 if ssh ${config.install.target} set -x ';' \
240 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
241 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
242 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
243 PATH=${
244 with pkgs;
245 lib.makeBinPath [
246 coreutils
247 nix
248 systemd
249 ]
250 }
251 sleep $((10 * 60))
252 ${profile}/bin/switch-to-configuration switch
253 systemctl reboot
254 '\'''' '&&' \
255 ${build.toplevel}/bin/switch-to-configuration test
256 then
257 ssh ${config.install.target} -o ControlPath=none set -x ';' \
258 systemctl stop nixos-fallback.service ';' \
259 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
260 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
261 nix-env --delete-generations 7d --profile ${profile}
262 else
263 tput rev
264 echo WARNING: switch-to-configuration was not registered at boot
265 tput sgr0
266 ssh ${config.install.target} -o ControlPath=none set -x ';' \
267 systemctl stop nixos-fallback.service
268 false
269 fi
270 '';
271 }
272 ) nixosConfigurations
273 );
274
275 # nix flake check
276 checks = forAllSystems (
277 args: with args; {
278 git-hooks-check = inputs.git-hooks.lib.${system}.run {
279 src = self;
280 hooks = {
281 nixfmt-rfc-style.enable = true;
282 };
283 };
284 }
285 );
286 };
287 }