]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
+dev/update(mermet)(postfix): rename sslKey and sslCert
[sourcephile-nix.git] / flake.nix
1 {
2 inputs = {
3 #doom-emacs.follows = "julm-nix/doom-emacs";
4 home-manager.follows = "julm-nix/home-manager";
5 #julm-nix.inputs.nixpkgs.follows = "nixpkgs";
6 julm-nix.url = "git+file:///home/julm/work/sourcephile/nix/julm-nix?ref=main";
7 nixpkgs.follows = "julm-nix/nixpkgs";
8 #nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
9 git-hooks.follows = "julm-nix/git-hooks";
10 };
11
12 outputs =
13 inputs:
14 let
15 remoteNixpkgsPatches = import (inputs.julm-nix + "/nixpkgs/patches.nix");
16 localNixpkgsPatches = [
17 #nixpkgs/patches/systemd-coredump.diff
18 ];
19 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
20 nixpkgsPath = originPkgs.applyPatches {
21 name = "nixpkgs-patched";
22 src = inputs.nixpkgs;
23 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
24 postPatch = ''
25 patch=$(printf '%s\n' ${
26 builtins.concatStringsSep " " (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)
27 } |
28 sort | sha256sum | cut -c -7)
29 echo "-patch-$patch" >.version-suffix
30 '';
31 };
32 profile = "/nix/var/nix/profiles/system";
33 inherit (inputs.nixpkgs) lib;
34 overlays =
35 args:
36 [
37 (finalPkgs: previousPkgs: { })
38 ]
39 ++ import (inputs.julm-nix + "/nixpkgs/overlays.nix") args
40 ++ import nixpkgs/overlays.nix args;
41 #nixosSystem = lib.nixosSystem;
42 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
43 forAllSystems =
44 f:
45 lib.genAttrs lib.systems.flakeExposed (
46 system:
47 f rec {
48 inherit system;
49 #pkgs = inputs.nixpkgs.legacyPackages.${system};
50 pkgs = import nixpkgsPath {
51 inherit system;
52 overlays = overlays { inherit inputs system; };
53 };
54 }
55 );
56 in
57 {
58 # nix -L build .#hello
59 packages = forAllSystems (args: with args; pkgs);
60
61 # nix -L build .#nixosConfigurations.mermet.config.system.build.toplevel
62 # nix -L build .#nixosConfigurations.mermet.config.boot.kernelPackages.kernel.configfile
63 # nix -L build .#nixosConfigurations.mermet.pkgs.hello
64 # nix eval --raw .#nixosConfigurations.mermet.config.networking.nftables.ruleset
65 nixosConfigurations =
66 lib.genAttrs
67 (builtins.attrNames (
68 lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))
69 ))
70 (
71 hostName:
72 nixosSystem {
73 system = null;
74 specialArgs = {
75 # Required to avoid infinite recursion
76 # when `inputs` is used in `imports`.
77 inherit inputs;
78 };
79 modules = [
80 nixos/default.nix
81 (inputs.julm-nix + "/nixos/default.nix")
82 (inputs.self + "/hosts/${hostName}.nix")
83 (
84 { config, ... }:
85 {
86 _module.args = {
87 inherit hostName;
88 hosts = inputs.self.nixosConfigurations;
89 host = inputs.self.nixosConfigurations.${hostName}._module.args;
90 info = import ./info.nix;
91 };
92 nixpkgs.overlays = overlays {
93 inherit inputs;
94 inherit (config.nixpkgs.hostPlatform) system;
95 };
96 #nixpkgs.buildPlatform = "x86_64-linux";
97 nixpkgs.config.allowUnfreePredicate =
98 pkg:
99 builtins.elem (lib.getName pkg) [
100 "canon-cups-ufr2"
101 "hplip"
102 ];
103 }
104 )
105 inputs.nixpkgs.nixosModules.notDetected
106 inputs.home-manager.nixosModules.home-manager
107 {
108 home-manager.useGlobalPkgs = true;
109 home-manager.useUserPackages = true;
110 home-manager.verbose = true;
111 home-manager.backupFileExtension = null;
112 home-manager.extraSpecialArgs = {
113 inherit hostName inputs;
114 };
115 }
116 {
117 programs.ssh.knownHosts = {
118 carotte = {
119 extraHostNames = [
120 "carotte"
121 "carotte.sourcephile.fr"
122 ];
123 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
124 };
125 mermet = {
126 extraHostNames = [
127 "mermet"
128 "mermet.sourcephile.fr"
129 ];
130 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
131 };
132 losurdo = {
133 extraHostNames = [
134 "losurdo"
135 "losurdo.sourcephile.fr"
136 ];
137 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
138 };
139 };
140 }
141 ];
142 }
143 );
144
145 # nix -L develop or direnv allow
146 devShell = forAllSystems (
147 { pkgs, system, ... }:
148 pkgs.callPackage ./shell.nix {
149 inherit
150 pkgs
151 inputs
152 system
153 nixpkgsPath
154 ;
155 inherit (inputs.self.checks.${system}.git-hooks-check) shellHook;
156 }
157 );
158
159 # nix -L run .#mermet.switch
160 apps = forAllSystems (
161 { pkgs, system, ... }:
162 with builtins;
163 mapAttrs (
164 hostName:
165 { config, pkgs, ... }:
166 let
167 inherit (config.system) build;
168 scriptApp = scriptName: ps: script: {
169 type = "app";
170 program =
171 (pkgs.writeShellScript "${hostName}-${scriptName}" ''
172 export PATH="${lib.makeBinPath ([ pkgs.buildPackages.coreutils ] ++ ps)}:$PATH"
173 set -eux
174 ${script}
175 '').outPath;
176 };
177 in
178 {
179 # Example: nix run .#mermet.switch
180 "switch" = scriptApp "switch" [ ] (''
181 set -o pipefail
182 shopt -s globstar
183
184 chmod -R g-rwx,o-rwx **/*.gpg
185 trap 'git reset **/*.gpg' EXIT
186 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
187
188 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
189 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
190
191 nix copy --to ssh://${config.install.target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
192
193 ${config.security.initrd.install}
194
195 if ssh ${config.install.target} set -x ';' \
196 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
197 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
198 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
199 PATH=${
200 with pkgs;
201 lib.makeBinPath [
202 coreutils
203 nix
204 systemd
205 ]
206 }
207 sleep $((10 * 60))
208 ${profile}/bin/switch-to-configuration switch
209 systemctl reboot
210 '\'''' '&&' \
211 ${build.toplevel}/bin/switch-to-configuration test
212 then
213 ssh ${config.install.target} -o ControlPath=none set -x ';' \
214 systemctl stop nixos-fallback.service ';' \
215 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
216 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
217 nix-env --delete-generations 7d --profile ${profile}
218 else
219 tput rev
220 echo WARNING: switch-to-configuration was not registered at boot
221 tput sgr0
222 ssh ${config.install.target} -o ControlPath=none set -x ';' \
223 systemctl stop nixos-fallback.service
224 false
225 fi
226 '');
227 # Example: nix run .#carotte.install-sd
228 # DELETEME: quite useless, better to use nixos-install
229 "install-sd" = scriptApp "install-sd" [ pkgs.zstd ] ''
230 set -o pipefail
231 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
232 unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
233 sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
234 '';
235 }
236 ) inputs.self.nixosConfigurations
237 );
238
239 # nix flake check
240 checks = forAllSystems (
241 args: with args; {
242 git-hooks-check = inputs.git-hooks.lib.${system}.run {
243 src = inputs.self;
244 hooks = {
245 nixfmt-rfc-style.enable = true;
246 };
247 };
248 }
249 );
250
251 };
252 }