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