]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
Add 1 git-crypt collaborator
[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/be6da3774db3746e6ae94bf412dd3707e35b2800";
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 systemd-creds, hence can go into the /nix/store
57 #credentials = private/credentials + "/${hostName}";
58 };
59 modules = cfg.modules ++ [
60 {
61 config._module.args = {
62 hosts = inputs.self.nixosConfigurations;
63 host = inputs.self.nixosConfigurations.${hostName}._module.args;
64 };
65 }
66 inputs.home-manager.nixosModules.home-manager
67 {
68 home-manager.useGlobalPkgs = true;
69 home-manager.useUserPackages = true;
70 home-manager.verbose = true;
71 home-manager.backupFileExtension = null;
72 home-manager.extraSpecialArgs = {
73 inherit hostName inputs;
74 };
75 }
76 ({ pkgs, ... }: {
77 nix.package = pkgs.nixUnstable;
78 nixpkgs.overlays =
79 import nixpkgs/overlays.nix ++
80 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
81 system.nixos.versionSuffix = ".${
82 lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${
83 inputs.self.shortRev or "dirty"}";
84 system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
85 # Let 'nixos-version --json' know about the Git revision of this flake.
86 system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
87 security.gnupg.agent.enable = true;
88 security.gnupg.store = inputs.pass + "/hosts/${hostName}";
89 /*
90 system.configurationRevision =
91 if inputs.self ? rev
92 then inputs.self.rev
93 else throw "Refusing to build from a dirty Git tree!";
94 */
95 programs.ssh.knownHosts = {
96 carotte = {
97 extraHostNames = [ "carotte" "carotte.sourcephile.fr" ];
98 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
99 };
100 mermet = {
101 extraHostNames = [ "mermet" "mermet.sourcephile.fr" ];
102 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
103 };
104 losurdo = {
105 extraHostNames = [ "losurdo" "losurdo.sourcephile.fr" ];
106 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
107 };
108 };
109 })
110 ];
111 }));
112 in
113 {
114 # Example: nix -L build .#nixosConfigurations.losurdo.config.system.build.toplevel
115 # Example: nix -L build .#nixosConfigurations.losurdo.config.boot.kernelPackages.kernel.configfile
116 # Example: nix -L build .#nixosConfigurations.losurdo.pkgs.hello
117 # Example: nix eval .#nixosConfigurations.losurdo.config.networking.hostName
118 nixosConfigurations = hosts {
119 carotte = hosts/carotte.nix;
120 losurdo = hosts/losurdo.nix;
121 mermet = hosts/mermet.nix;
122 };
123 #nixosModule = import nixos/modules.nix;
124 }
125 // inputs.flake-utils.lib.eachDefaultSystem (system:
126 #let pkgs = inputs.nixpkgs.legacyPackages.${system}; in
127 let
128 pkgs = import nixpkgsPath {
129 inherit system;
130 overlays =
131 import nixpkgs/overlays.nix ++
132 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
133 };
134 in {
135 legacyPackages = pkgs;
136 devShell = import ./shell.nix { inherit inputs pkgs; };
137 apps = builtins.mapAttrs (hostName: { config, ... }: let
138 build = config.system.build;
139 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
140 profile = "/nix/var/nix/profiles/system";
141 in rec {
142 # Example: nix run .#losurdo.switch
143 "switch" = {
144 type = "app";
145 program = (pkgs.writeShellScript "switch" (''
146 set -eux
147 set -o pipefail
148 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
149 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
150 ${sendkeys.program}
151 '' + lib.optionalString config.boot.initrd.network.ssh.enable ''
152 # Send the SSH key of the initrd
153 gpg --decrypt '${config.security.gnupg.store}/initrd/ssh.key.gpg' |
154 ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
155 # Send the Wireguard key of the initrd
156 gpg --decrypt '${config.security.gnupg.store}/wireguard/wg-intra/privateKey.gpg' |
157 ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/wg-intra.key
158 '' + ''
159 ssh ${target} \
160 nix-env --profile '${profile}' --set '${build.toplevel}' '&&' \
161 '${profile}'/bin/switch-to-configuration switch
162 ''
163 )).outPath;
164 };
165 # Example: nix run .#carotte.install-sd
166 "install-sd" = {
167 type = "app";
168 program = (pkgs.writeShellScript "install-sd" ''
169 export PATH="$PATH:${with pkgs; lib.makeBinPath [coreutils zstd]}"
170 set -eux
171 set -o pipefail
172 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
173 unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
174 sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
175 '').outPath;
176 };
177 # Example: nix run .#losurdo.sendkeys
178 "sendkeys" = {
179 type = "app";
180 #program = config.security.gnupg.agent.sendKeys + "/bin/gnupg-agent-sendKeys";
181 program = (pkgs.writeShellScript "sendkeys" ''
182 set -eux
183 chmod -R g-rwx,o-rwx "$PWD/secrets/hosts"
184 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
185 trap 'git reset secrets/hosts' EXIT
186 git rm -rf --cached --ignore-unmatch secrets/hosts # prevent copying to /nix/store
187 ${pkgs.bash}/bin/bash -eux ${config.security.gnupg.agent.sendKeys + "/bin/gnupg-agent-sendKeys"}
188 '').outPath;
189 };
190 }) inputs.self.nixosConfigurations;}
191 );
192 }