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