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