]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
losurdo: syncoid: move to LoadCredentialEncrypted=
[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/495b19d5b3e62b4ec7e846bdfb6ef3d9c3b83492";
4 #inputs.nixpkgs.url = "flake:nixpkgs";
5 inputs.flake-utils.url = "github:numtide/flake-utils";
6 inputs.home-manager.follows = "julm-nix/home-manager";
7 #inputs.julm-nix.url = "git+https://git.hut.sourcephile.fr/~julm/julm-nix?ref=main";
8 inputs.julm-nix.url = "git+file:///home/julm/work/sourcephile/nix/julm-nix?ref=main";
9 #inputs.julm-nix.type = "path";
10 #inputs.julm-nix.path = "/home/julm/work/sourcephile/nix/julm-nix";
11 inputs.julm-nix.inputs.flake-utils.follows = "flake-utils";
12 inputs.julm-nix.inputs.nixpkgs.follows = "nixpkgs";
13 inputs.doom-emacs.follows = "julm-nix/doom-emacs";
14 inputs.nix-doom-emacs.follows = "julm-nix/nix-doom-emacs";
15 inputs.nix-doom-emacs.inputs.doom-emacs.follows = "julm-nix/doom-emacs";
16 inputs.emacs-overlay.follows = "julm-nix/emacs-overlay";
17 #inputs.shell = { type = "path"; path = "./shell"; flake = false; };
18 #inputs.secrets = { type = "path"; path = "./sec"; flake = false; };
19 #inputs.pass = { type = "path"; path = "./pass"; flake = false; };
20 inputs.shell.url = "path:./shell";
21 inputs.shell.flake = false;
22 inputs.secrets.type = "path";
23 inputs.secrets.path = "/home/julm/work/sourcephile/nix/sec";
24 inputs.secrets.flake = false;
25 inputs.pass.type = "path";
26 inputs.pass.path = "/home/julm/work/sourcephile/nix/pass";
27 inputs.pass.flake = false;
28
29 outputs = inputs: let
30 remoteNixpkgsPatches = import nixpkgs/patches.nix;
31 localNixpkgsPatches = [
32 #nixpkgs/patches/sourcehut.diff
33 #nixpkgs/patches/bitmask.diff
34 #nixpkgs/patches/systemd-coredump.diff
35 ];
36 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
37 nixpkgsPath = originPkgs.applyPatches {
38 name = "nixpkgs-patched";
39 src = inputs.nixpkgs;
40 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
41 postPatch = ''
42 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
43 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
44 sort | sha256sum | cut -c -7)
45 echo "+patch-$patch" >.version-suffix
46 '';
47 };
48 lib = originPkgs.lib;
49 hosts = builtins.mapAttrs (hostName: hostConfig:
50 let cfg = import hostConfig { inherit inputs hostName nixpkgsPath; }; in
51 import (nixpkgsPath + "/nixos/lib/eval-config.nix") (lib.recursiveUpdate cfg {
52 specialArgs = {
53 inherit hostName inputs;
54 # Encrypted using git-crypt, hence kept out of /nix/store
55 private = "/root/private";
56 # Encrypted using git-crypt, but may go into /nix/store
57 shared = private/shared;
58 # Encrypted using systemd-creds, hence can go into the /nix/store
59 credentials = private/hosts + "/${hostName}/credentials";
60 };
61 modules = cfg.modules ++ [
62 {
63 config._module.args = {
64 hosts = inputs.self.nixosConfigurations;
65 host = inputs.self.nixosConfigurations.${hostName}._module.args;
66 };
67 }
68 inputs.home-manager.nixosModules.home-manager
69 {
70 home-manager.useGlobalPkgs = true;
71 home-manager.useUserPackages = true;
72 home-manager.verbose = true;
73 home-manager.backupFileExtension = null;
74 home-manager.extraSpecialArgs = {
75 inherit hostName inputs;
76 };
77 }
78 ({ pkgs, ... }: {
79 nix.package = pkgs.nixUnstable;
80 nixpkgs.overlays =
81 import nixpkgs/overlays.nix ++
82 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
83 system.nixos.versionSuffix = ".${
84 lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${
85 inputs.self.shortRev or "dirty"}";
86 system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
87 # Let 'nixos-version --json' know about the Git revision of this flake.
88 system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
89 security.gnupg.agent.enable = true;
90 security.gnupg.store = inputs.pass + "/hosts/${hostName}";
91 /*
92 system.configurationRevision =
93 if inputs.self ? rev
94 then inputs.self.rev
95 else throw "Refusing to build from a dirty Git tree!";
96 */
97 programs.ssh.knownHosts = {
98 carotte = {
99 extraHostNames = [ "carotte" "carotte.sourcephile.fr" ];
100 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
101 };
102 mermet = {
103 extraHostNames = [ "mermet" "mermet.sourcephile.fr" ];
104 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
105 };
106 losurdo = {
107 extraHostNames = [ "losurdo" "losurdo.sourcephile.fr" ];
108 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
109 };
110 };
111 })
112 ];
113 }));
114 in
115 {
116 # Example: nix -L build .#nixosConfigurations.losurdo.config.system.build.toplevel
117 # Example: nix -L build .#nixosConfigurations.losurdo.config.boot.kernelPackages.kernel.configfile
118 # Example: nix -L build .#nixosConfigurations.losurdo.pkgs.hello
119 # Example: nix eval .#nixosConfigurations.losurdo.config.networking.hostName
120 nixosConfigurations = hosts {
121 carotte = hosts/carotte.nix;
122 losurdo = hosts/losurdo.nix;
123 mermet = hosts/mermet.nix;
124 };
125 #nixosModule = import nixos/modules.nix;
126 }
127 // inputs.flake-utils.lib.eachDefaultSystem (system:
128 #let pkgs = inputs.nixpkgs.legacyPackages.${system}; in
129 let
130 pkgs = import nixpkgsPath {
131 inherit system;
132 overlays =
133 import nixpkgs/overlays.nix ++
134 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
135 };
136 in {
137 legacyPackages = pkgs;
138 devShell = import ./shell.nix { inherit inputs pkgs; };
139 apps = builtins.mapAttrs (hostName: { config, ... }: let
140 build = config.system.build;
141 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
142 profile = "/nix/var/nix/profiles/system";
143 in rec {
144 # Example: nix run .#losurdo.switch
145 "switch" = {
146 type = "app";
147 program = (pkgs.writeShellScript "switch" (''
148 set -eux
149 set -o pipefail
150 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
151 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
152 ${sendkeys.program}
153 '' + lib.optionalString config.boot.initrd.network.ssh.enable ''
154 # Send the SSH key of the initrd
155 gpg --decrypt '${config.security.gnupg.store}/initrd/ssh.key.gpg' |
156 ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
157 # Send the Wireguard key of the initrd
158 gpg --decrypt '${config.security.gnupg.store}/wireguard/wg-intra/privateKey.gpg' |
159 ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/wg-intra.key
160 '' + ''
161 ssh ${target} \
162 nix-env --profile '${profile}' --set '${build.toplevel}' '&&' \
163 '${profile}'/bin/switch-to-configuration switch
164 ''
165 )).outPath;
166 };
167 # Example: nix run .#carotte.install-sd
168 "install-sd" = {
169 type = "app";
170 program = (pkgs.writeShellScript "install-sd" ''
171 export PATH="$PATH:${with pkgs; lib.makeBinPath [coreutils zstd]}"
172 set -eux
173 set -o pipefail
174 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
175 unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
176 sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
177 '').outPath;
178 };
179 # Example: nix run .#losurdo.sendkeys
180 "sendkeys" = {
181 type = "app";
182 #program = config.security.gnupg.agent.sendKeys + "/bin/gnupg-agent-sendKeys";
183 program = (pkgs.writeShellScript "sendkeys" ''
184 set -eux
185 chmod -R g-rwx,o-rwx "$PWD/secrets/hosts"
186 rsync -ai --no-times --numeric-ids --usermap=:root --groupmap=:root --chmod=Du+rwx,Fu=r,go-rwx --delete "$PWD/secrets/hosts/${hostName}/root/" ${target}:/root/secrets
187 trap 'git reset secrets/hosts' EXIT
188 git rm -rf --cached --ignore-unmatch secrets/hosts # prevent copying to /nix/store
189 ${pkgs.bash}/bin/bash -eux ${config.security.gnupg.agent.sendKeys + "/bin/gnupg-agent-sendKeys"}
190 '').outPath;
191 };
192 }) inputs.self.nixosConfigurations;}
193 );
194 }