]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
nix: update input julm-nix
[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 system:
36 [
37 (finalPkgs: previousPkgs: { })
38 ]
39 ++ import (inputs.julm-nix + "/nixpkgs/overlays.nix")
40 ++ import nixpkgs/overlays.nix;
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 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 config.nixpkgs.hostPlatform.system;
93 #nixpkgs.buildPlatform = "x86_64-linux";
94 nixpkgs.config.allowUnfreePredicate =
95 pkg:
96 builtins.elem (lib.getName pkg) [
97 "canon-cups-ufr2"
98 "hplip"
99 ];
100 }
101 )
102 inputs.nixpkgs.nixosModules.notDetected
103 inputs.home-manager.nixosModules.home-manager
104 {
105 home-manager.useGlobalPkgs = true;
106 home-manager.useUserPackages = true;
107 home-manager.verbose = true;
108 home-manager.backupFileExtension = null;
109 home-manager.extraSpecialArgs = {
110 inherit hostName inputs;
111 };
112 }
113 {
114 programs.ssh.knownHosts = {
115 carotte = {
116 extraHostNames = [
117 "carotte"
118 "carotte.sourcephile.fr"
119 ];
120 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
121 };
122 mermet = {
123 extraHostNames = [
124 "mermet"
125 "mermet.sourcephile.fr"
126 ];
127 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
128 };
129 losurdo = {
130 extraHostNames = [
131 "losurdo"
132 "losurdo.sourcephile.fr"
133 ];
134 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
135 };
136 };
137 }
138 ];
139 }
140 );
141
142 # nix -L develop or direnv allow
143 devShell = forAllSystems (
144 { pkgs, system, ... }:
145 pkgs.callPackage ./shell.nix {
146 inherit
147 pkgs
148 inputs
149 system
150 nixpkgsPath
151 ;
152 inherit (inputs.self.checks.${system}.git-hooks-check) shellHook;
153 }
154 );
155
156 # nix -L run .#mermet.switch
157 apps = forAllSystems (
158 { pkgs, system, ... }:
159 with builtins;
160 mapAttrs (
161 hostName:
162 { config, pkgs, ... }:
163 let
164 inherit (config.system) build;
165 scriptApp = scriptName: ps: script: {
166 type = "app";
167 program =
168 (pkgs.writeShellScript "${hostName}-${scriptName}" ''
169 export PATH="${lib.makeBinPath ([ pkgs.buildPackages.coreutils ] ++ ps)}:$PATH"
170 set -eux
171 ${script}
172 '').outPath;
173 };
174 in
175 {
176 # Example: nix run .#mermet.switch
177 "switch" = scriptApp "switch" [ ] (''
178 set -o pipefail
179 shopt -s globstar
180
181 chmod -R g-rwx,o-rwx **/*.gpg
182 trap 'git reset **/*.gpg' EXIT
183 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
184
185 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
186 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
187
188 nix copy --to ssh://${config.install.target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
189
190 ${config.security.initrd.install}
191
192 if ssh ${config.install.target} set -x ';' \
193 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
194 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
195 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
196 PATH=${
197 with pkgs;
198 lib.makeBinPath [
199 coreutils
200 nix
201 systemd
202 ]
203 }
204 sleep $((10 * 60))
205 ${profile}/bin/switch-to-configuration switch
206 systemctl reboot
207 '\'''' '&&' \
208 ${build.toplevel}/bin/switch-to-configuration test
209 then
210 ssh ${config.install.target} -o ControlPath=none set -x ';' \
211 systemctl stop nixos-fallback.service ';' \
212 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
213 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
214 nix-env --delete-generations 7d --profile ${profile}
215 else
216 tput rev
217 echo WARNING: switch-to-configuration was not registered at boot
218 tput sgr0
219 ssh ${config.install.target} -o ControlPath=none set -x ';' \
220 systemctl stop nixos-fallback.service
221 false
222 fi
223 '');
224 # Example: nix run .#carotte.install-sd
225 # DELETEME: quite useless, better to use nixos-install
226 "install-sd" = scriptApp "install-sd" [ pkgs.zstd ] ''
227 set -o pipefail
228 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
229 unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
230 sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
231 '';
232 }
233 ) inputs.self.nixosConfigurations
234 );
235
236 # nix flake check
237 checks = forAllSystems (
238 args: with args; {
239 git-hooks-check = inputs.git-hooks.lib.${system}.run {
240 src = inputs.self;
241 hooks = {
242 nixfmt-rfc-style.enable = true;
243 };
244 };
245 }
246 );
247
248 };
249 }