]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
nix: polish flake.nix
[sourcephile-nix.git] / flake.nix
1 {
2 inputs.nixpkgs.url = "github:NixOS/nixpkgs/a332da8588aeea4feb9359d23f58d95520899e3c";
3 inputs.flake-utils.url = "github:numtide/flake-utils";
4 inputs.shell = { type = "path"; path = "./shell"; flake = false; };
5 inputs.secrets = { type = "path"; path = "./sec"; flake = false; };
6 inputs.pass = { type = "path"; path = "./pass"; flake = false; };
7 outputs = flakes: let
8 remoteNixpkgsPatches = [
9 { meta.description = "dstat: fix pluginpath";
10 url = "https://github.com/NixOS/nixpkgs/pull/80151.diff";
11 sha256 = "0jjw2gvp7b7v2n2m2d6yj0gw711j6p9lyjf5ywp2y9ql6905qf4b";
12 }
13 { meta.description = "syncoid: fix PATH to let it use sudo";
14 url = "https://github.com/NixOS/nixpkgs/pull/83901.diff";
15 sha256 = "0q2dicmvl3h3hb9xdd870n5hf6lac489p000c7f1r6k70sh2id4l";
16 }
17 { meta.description = "sanoid: fix sanoid.conf generation";
18 url = "https://github.com/NixOS/nixpkgs/pull/83904.diff";
19 sha256 = "Sy9wPmL+Lfl7hMEeXYOEMk3KlfdL21aL92v6MiGajds=";
20 }
21 { meta.description = "nixos/public-inbox: init";
22 url = "https://github.com/NixOS/nixpkgs/pull/77450.diff";
23 sha256 = "13ikg7chpbf6rrg5sngbdb95q3awhdgl4g8vci42xmqyf208hzzd";
24 }
25 { meta.description = "apparmor: fix and improve the service";
26 url = "https://github.com/NixOS/nixpkgs/pull/93457.diff";
27 sha256 = "8QNOKDWNJiZujAvmDtZPZI5VRc+oh40IqjTYn/RCUi4=";
28 }
29 { meta.description = "nixos/security.gnupg: provisioning GnuPG-protected secrets through the Nix store";
30 url = "https://github.com/NixOS/nixpkgs/pull/93659.diff";
31 sha256 = "8prVG0VYXsTPwnGyAv7FlMh2M5MENX1NxkX/ql1lmHs=";
32 }
33 { meta.description = "nixos/croc: init";
34 url = "https://github.com/NixOS/nixpkgs/pull/93629.diff";
35 sha256 = "0fv3lpj244hvxyixxv4akrr70jv5wwbhb3kmbmd2yskx59a71rch";
36 }
37 { meta.description = "nixos/biboumi: init";
38 url = "https://github.com/NixOS/nixpkgs/pull/94917.diff";
39 sha256 = "1rjmmkx1pm08qi0d0bg6lr60jh8w1ifa8n4gnms4k6wka233yk0z";
40 }
41 { meta.description = "dovecot_fts_xapian: 1.3.1 -> 1.3.3";
42 url = "https://github.com/NixOS/nixpkgs/pull/94938.diff";
43 sha256 = "10bjwcwpvq7nnqdpz0n7c61kb3b27v1abyc80pki7d13jmzzjc04";
44 }
45 { meta.description = "nixos-install: add support for flakes";
46 url = "https://github.com/NixOS/nixpkgs/pull/95194.diff";
47 sha256 = "Fk3AFiY7uRx+8qqX4WvusMYGJ02Xd4PyzLVY5Hzx8gQ=";
48 }
49 ];
50 localNixpkgsPatches = [
51 #nixpkgs/patches/security.apparmor.diff
52 #nixpkgs/patches/security.gnupg.diff
53 #nixpkgs/patches/fix-ld-nix.diff
54 #nixpkgs/patches/fix-ld-nix-apparmor.diff
55 ];
56 originPkgs = flakes.nixpkgs.legacyPackages."x86_64-linux";
57 nixpkgs = originPkgs.applyPatches {
58 name = "nixpkgs-patched";
59 src = flakes.nixpkgs;
60 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
61 postPatch = ''
62 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
63 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
64 sort | sha256sum | cut -c -7)
65 echo "+patch-$patch" >.version-suffix
66 '';
67 };
68 lib = originPkgs.lib;
69 machines = builtins.mapAttrs (machineName: machineConfig:
70 let cfg = import machineConfig { inherit flakes; }; in
71 import (nixpkgs + "/nixos/lib/eval-config.nix") (cfg // {
72 extraArgs = {
73 inherit machineName flakes;
74 machines = flakes.self.nixosConfigurations;
75 } // (cfg.extraArgs or {});
76 modules = cfg.modules ++ [({pkgs, ...}: {
77 nix.registry.nixpkgs.flake = nixpkgs;
78 nix.package = pkgs.nixFlakes;
79 nix.extraOptions = "experimental-features = nix-command flakes";
80 nixpkgs.overlays = import nixpkgs/overlays.nix;
81 system.nixos.versionSuffix = ".${
82 lib.substring 0 8 (flakes.self.lastModifiedDate or flakes.self.lastModified)}.${
83 flakes.self.shortRev or "dirty"}";
84 system.nixos.revision = lib.mkIf (flakes.self ? rev) flakes.self.rev;
85 # Let 'nixos-version --json' know about the Git revision of this flake.
86 system.configurationRevision = lib.mkIf (flakes.self ? rev) flakes.self.rev;
87 /*
88 system.configurationRevision =
89 if flakes.self ? rev
90 then flakes.self.rev
91 else throw "Refusing to build from a dirty Git tree!";
92 */
93 })];
94 }));
95 in
96 {
97 nixosConfigurations = machines {
98 losurdo = machines/losurdo.nix;
99 mermet = machines/mermet.nix;
100 };
101 }
102 // flakes.flake-utils.lib.eachDefaultSystem (system:
103 #let pkgs = flakes.nixpkgs.legacyPackages.${system}; in
104 let
105 pkgs = import nixpkgs {
106 inherit system;
107 config = {}; # Make the config pure, ignoring user's config.
108 overlays = import nixpkgs/overlays.nix;
109 #overlays = import (flakes.self + "/nixpkgs/overlays.nix");
110 };
111 in {
112 devShell = import ./shell.nix { inherit flakes pkgs; };
113 apps = builtins.mapAttrs (machineName: { config, ... }: {
114 type = "app";
115 program = (let
116 system = config.system.build.toplevel;
117 rootKey = "root/key";
118 initrdKey = "initrd/ssh.key";
119 keygrip = builtins.getAttr machineName {
120 losurdo = "9AA84E6F6D71F9163C46BF396B141A0806219077";
121 mermet = "89F52A879E0019A966503AFFDE72EEA84CDFA3A7";
122 };
123 target = "root@${config.networking.hostName}.${config.networking.domain}";
124 nixCopyFlags = ["--substitute-on-destination"];
125 profile = "/nix/var/nix/profiles/system";
126 in pkgs.writeShellScript "install" ''
127 set -eux
128 ssh '${target}' \
129 ${pkgs.gnupg}/bin/gpg-connect-agent --no-autostart --homedir /var/lib/gnupg "'keyinfo --list'" /bye 2>&1 |
130 grep -qx -e "gpg-connect-agent: no gpg-agent running in this session" \
131 -e "S KEYINFO ${keygrip} . . . 1 .*" || {
132 # Send the rootKey
133 gpg --decrypt '${config.security.gnupg.store}/${rootKey}.pass.gpg' |
134 gpg --batch --pinentry-mode loopback --passphrase-fd 0 --export-secret-subkeys @root@${machineName} |
135 ssh '${target}' \
136 gpg --no-autostart --homedir /var/lib/gnupg --no-autostart --batch --pinentry-mode loopback --import
137
138 # Send the rootKey's passphrase
139 gpg --decrypt '${config.security.gnupg.store}/${rootKey}.pass.gpg' |
140 ssh '${target}' \
141 gpg-preset-passphrase --homedir /var/lib/gnupg --preset ${keygrip}
142 }
143 # Send the SSH key of the initrd
144 gpg --decrypt '${config.security.gnupg.store}/${initrdKey}.gpg' |
145 ssh '${target}' \
146 install -D -m 400 -o root -g root /dev/stdin /root/${initrdKey}
147 ${pkgs.nixFlakes}/bin/nix copy --to ssh://'${target}' \
148 ${lib.escapeShellArgs nixCopyFlags} ${system}
149 ssh '${target}' \
150 nix-env --profile '${profile}' --set '${system}' '&&' \
151 '${profile}'/bin/switch-to-configuration switch
152 '').outPath;
153 }) flakes.self.nixosConfigurations;
154 }
155 );
156 }