]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
syncoid: fix keys rights and known_hosts
[sourcephile-nix.git] / flake.nix
1 {
2 # Pin down nixpkgs from github, instead of using global, system or user registries.
3 inputs.nixpkgs.url = "github:NixOS/nixpkgs/b69f140a8028e4718bde4e5082f8df38e50b7971";
4 #inputs.nixpkgs.url = "flake:nixpkgs";
5 inputs.flake-utils.url = "github:numtide/flake-utils";
6 inputs.shell = { type = "path"; path = "./shell"; flake = false; };
7 inputs.secrets = { type = "path"; path = "./sec"; flake = false; };
8 inputs.pass = { type = "path"; path = "./pass"; flake = false; };
9 outputs = inputs: let
10 remoteNixpkgsPatches = [
11 { meta.description = "syncoid: fix PATH to let it use sudo";
12 url = "https://github.com/NixOS/nixpkgs/pull/83901.diff";
13 sha256 = "0q2dicmvl3h3hb9xdd870n5hf6lac489p000c7f1r6k70sh2id4l";
14 }
15 { meta.description = "sanoid: fix sanoid.conf generation";
16 url = "https://github.com/NixOS/nixpkgs/pull/83904.diff";
17 sha256 = "Sy9wPmL+Lfl7hMEeXYOEMk3KlfdL21aL92v6MiGajds=";
18 }
19 { meta.description = "nixos/public-inbox: init";
20 url = "https://github.com/NixOS/nixpkgs/pull/77450.diff";
21 sha256 = "13ikg7chpbf6rrg5sngbdb95q3awhdgl4g8vci42xmqyf208hzzd";
22 }
23 { meta.description = "nixos/security.gnupg: provisioning GnuPG-protected secrets through the Nix store";
24 url = "https://github.com/NixOS/nixpkgs/pull/93659.diff";
25 sha256 = "3im5nSrlM32DQUeq0Yp1MHkUcQyLdCGbxfJjgcc9e78=";
26 }
27 { meta.description = "nixos/croc: init";
28 url = "https://github.com/NixOS/nixpkgs/pull/93629.diff";
29 sha256 = "sha256-hzs5Z9cYCo6aNFfylB37W+1neejV4/kVyM1QmqVhVww=";
30 }
31 { meta.description = "dovecot_fts_xapian: 1.3.1 -> 1.3.3";
32 url = "https://github.com/NixOS/nixpkgs/pull/94938.diff";
33 sha256 = "10bjwcwpvq7nnqdpz0n7c61kb3b27v1abyc80pki7d13jmzzjc04";
34 }
35 { meta.description = "transmission: use freeformType on settings";
36 url = "https://github.com/NixOS/nixpkgs/pull/96655.diff";
37 sha256 = "sha256-lRKe8WnGPc8ojaD9W8ZS+NVMhIoGOCOj9njhqQhzaCM";
38 }
39 { meta.description = "tor: improve type-checking and hardening";
40 url = "https://github.com/NixOS/nixpkgs/pull/97740.diff";
41 sha256 = "sha256-ojmDxbGtfsvrUCtjkR7ZynayEGNDLwYl2Ea6yLOQpIk=";
42 }
43 /*
44 { meta.description = "nixos/postfix: add sandbox";
45 url = "https://github.com/NixOS/nixpkgs/pull/93305.diff";
46 sha256 = "sha256-KmoaPj8G4kRlwkBKo/giyST8xQfnjX3OJhh68NKNaRI=";
47 }
48 { meta.description = "nixos/rspamd: add sandbox";
49 url = "https://github.com/NixOS/nixpkgs/pull/93293.diff";
50 sha256 = "sha256-7OfBzJPeGVhHf4ziYLwOpqaYbElCsjLkT4Y/bbCOPx0=";
51 }
52 */
53 { meta.description = "nixos/syncoid: split in multiple systemd services";
54 url = "https://github.com/NixOS/nixpkgs/pull/98455.diff";
55 sha256 = "sha256-vmPzvjwAOSWqfizgDk3l6FrMD2OsG+8UpIkQvOUcXNI=";
56 }
57 { meta.description = "zerobin: update to v1.0.5";
58 url = "https://github.com/NixOS/nixpkgs/pull/98734.diff";
59 sha256 = "sha256-mEFvzR0ymwR7V3bJBnr9fR3GxA1jDfXZXQzo+jc5Nxc=";
60 }
61 /*
62 { meta.description = "apparmor";
63 url = "https://github.com/NixOS/nixpkgs/pull/93457.diff";
64 sha256 = "sha256-JzS6/42AS9f0XUuC8ybw1tR8lpFwOC59BNqYN4mmeVI=";
65 }
66 */
67 ];
68 localNixpkgsPatches = [
69 nixpkgs/patches/apparmor.diff
70 #nixpkgs/patches/zerobin.diff
71 #nixpkgs/patches/gitolite.diff
72 #nixpkgs/patches/tor.diff
73 #nixpkgs/patches/fix-ld-nix.diff
74 #nixpkgs/patches/fix-ld-nix-apparmor.diff
75 ];
76 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
77 nixpkgs = originPkgs.applyPatches {
78 name = "nixpkgs-patched";
79 src = inputs.nixpkgs;
80 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
81 postPatch = ''
82 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
83 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
84 sort | sha256sum | cut -c -7)
85 echo "+patch-$patch" >.version-suffix
86 '';
87 };
88 lib = originPkgs.lib;
89 machines = builtins.mapAttrs (machineName: machineConfig:
90 let cfg = import machineConfig { inherit inputs; }; in
91 import (nixpkgs + "/nixos/lib/eval-config.nix") (cfg // {
92 extraArgs = {
93 inherit machineName inputs;
94 machines = inputs.self.nixosConfigurations;
95 } // (cfg.extraArgs or {});
96 modules = cfg.modules ++ [({pkgs, ...}: {
97 nix.registry.nixpkgs.flake = nixpkgs;
98 nix.package = pkgs.nixFlakes;
99 nix.extraOptions = "experimental-features = nix-command flakes";
100 nixpkgs.overlays = import nixpkgs/overlays.nix;
101 system.nixos.versionSuffix = ".${
102 lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${
103 inputs.self.shortRev or "dirty"}";
104 system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
105 # Let 'nixos-version --json' know about the Git revision of this flake.
106 system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
107 boot.initrd.network.ssh.hostKeys = [ "/root/initrd/ssh.key" ];
108 /*
109 system.configurationRevision =
110 if inputs.self ? rev
111 then inputs.self.rev
112 else throw "Refusing to build from a dirty Git tree!";
113 */
114 programs.ssh.knownHosts = {
115 mermet = {
116 hostNames = [ "mermet" "mermet.sourcephile.fr" ];
117 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
118 };
119 losurdo = {
120 hostNames = [ "losurdo" "losurdo.sourcephile.fr" ];
121 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
122 };
123 };
124 })];
125 }));
126 in
127 {
128 # nix -L build .#nixosConfigurations.${machineName}.config.system.build.toplevel
129 nixosConfigurations = machines {
130 losurdo = machines/losurdo.nix;
131 mermet = machines/mermet.nix;
132 };
133 }
134 // inputs.flake-utils.lib.eachDefaultSystem (system:
135 #let pkgs = inputs.nixpkgs.legacyPackages.${system}; in
136 let
137 pkgs = import nixpkgs {
138 inherit system;
139 overlays = import nixpkgs/overlays.nix;
140 };
141 in {
142 legacyPackages = pkgs;
143 devShell = import ./shell.nix { inherit inputs pkgs; };
144 apps = builtins.mapAttrs (machineName: { config, ... }: let
145 system = config.system.build.toplevel;
146 keygrip = builtins.getAttr machineName {
147 losurdo = "9AA84E6F6D71F9163C46BF396B141A0806219077";
148 mermet = "89F52A879E0019A966503AFFDE72EEA84CDFA3A7";
149 };
150 target = "root@${config.networking.hostName}.${config.networking.domain}";
151 nixCopyFlags = ["--substitute-on-destination"];
152 profile = "/nix/var/nix/profiles/system";
153 in rec {
154 # Example: nix run .#losurdo.switch
155 "switch" = {
156 type = "app";
157 program = (pkgs.writeShellScript "switch" ''
158 set -eux
159 set -o pipefail
160 nix-store --add-root machines/${machineName}.root --indirect --realise ${system}
161 ${pkgs.nixFlakes}/bin/nix copy --to ssh://'${target}' \
162 ${lib.escapeShellArgs nixCopyFlags} ${system}
163 ${sendkeys.program}
164 # Send the SSH key of the initrd
165 gpg --decrypt '${config.security.gnupg.store}/initrd/ssh.key.gpg' |
166 ssh '${target}' install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
167 # Send the Wireguard key of the initrd
168 gpg --decrypt '${config.security.gnupg.store}/wireguard/wg-intra/privateKey.gpg' |
169 ssh '${target}' install -D -m 400 -o root -g root /dev/stdin /root/initrd/wg-intra.key
170 ssh '${target}' \
171 nix-env --profile '${profile}' --set '${system}' '&&' \
172 '${profile}'/bin/switch-to-configuration switch
173 '').outPath;
174 };
175 # Example: nix run .#losurdo.sendkeys
176 "sendkeys" = {
177 type = "app";
178 program = (pkgs.writeShellScript "sendkeys" ''
179 set -eux
180 set -o pipefail
181 ssh '${target}' \
182 ${pkgs.gnupg}/bin/gpg-connect-agent --no-autostart --homedir /var/lib/gnupg "'keyinfo --list'" /bye 2>&1 |
183 grep -qx -e "gpg-connect-agent: no gpg-agent running in this session" \
184 -e "S KEYINFO ${keygrip} . . . 1 .*" || {
185 # Send the GnuPG root key
186 gpg --decrypt '${config.security.gnupg.store}/root/key.pass.gpg' |
187 gpg --batch --pinentry-mode loopback --passphrase-fd 0 --export-secret-subkeys @root@${machineName} |
188 ssh '${target}' \
189 gpg --no-autostart --homedir /var/lib/gnupg --no-autostart --batch --pinentry-mode loopback --import
190
191 # Send the GnuPG root key's passphrase
192 gpg --decrypt '${config.security.gnupg.store}/root/key.pass.gpg' |
193 ssh '${target}' \
194 gpg-preset-passphrase --homedir /var/lib/gnupg --preset ${keygrip}
195 }
196 '').outPath;
197 };
198 }) inputs.self.nixosConfigurations;}
199 );
200 }