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