]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
losurdo: creds: fix paths and other stuffs
[sourcephile-nix.git] / flake.nix
1 {
2 inputs = {
3 doom-emacs.follows = "julm-nix/doom-emacs";
4 home-manager.follows = "julm-nix/home-manager";
5 julm-nix.url = "git+file:///home/julm/work/sourcephile/nix/julm-nix?ref=main";
6 julm-nix.inputs.nixpkgs.follows = "nixpkgs";
7 nix-formatter-pack.follows = "julm-nix/nix-formatter-pack";
8 nixpkgs.url = "github:NixOS/nixpkgs/fdebb81f45a1ba2c4afca5fd9f526e1653ad0949";
9 pre-commit-hooks.follows = "julm-nix/pre-commit-hooks";
10 };
11
12 outputs = inputs: let
13 remoteNixpkgsPatches = import (inputs.julm-nix + "/nixpkgs/patches.nix");
14 localNixpkgsPatches = [
15 #nixpkgs/patches/systemd-coredump.diff
16 ];
17 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
18 nixpkgsPath = originPkgs.applyPatches {
19 name = "nixpkgs-patched";
20 src = inputs.nixpkgs;
21 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
22 postPatch = ''
23 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
24 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
25 sort | sha256sum | cut -c -7)
26 echo "-patch-$patch" >.version-suffix
27 '';
28 };
29 profile = "/nix/var/nix/profiles/system";
30 inherit (inputs.nixpkgs) lib;
31 #nixosSystem = lib.nixosSystem;
32 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
33 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
34 inherit system;
35 #pkgs = inputs.nixpkgs.legacyPackages.${system};
36 pkgs = import nixpkgsPath {
37 inherit system;
38 overlays =
39 import nixpkgs/overlays.nix ++
40 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
41 };
42 });
43 in
44 {
45 # nix -L build .#nixosConfigurations.mermet.config.system.build.toplevel
46 # nix -L build .#nixosConfigurations.mermet.config.boot.kernelPackages.kernel.configfile
47 # nix -L build .#nixosConfigurations.mermet.pkgs.hello
48 # nix eval --raw .#nixosConfigurations.mermet.config.networking.nftables.rulese
49 nixosConfigurations = lib.genAttrs
50 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
51 (hostName: nixosSystem
52 {
53 system = null;
54 specialArgs = {
55 # Required to avoid infinite recursion
56 # when `inputs` is used in `imports`.
57 inherit inputs;
58 };
59 modules = [
60 nixos/default.nix
61 (inputs.self + "/hosts/${hostName}.nix")
62 {
63 _module.args = {
64 inherit hostName;
65 hosts = inputs.self.nixosConfigurations;
66 host = inputs.self.nixosConfigurations.${hostName}._module.args;
67 };
68 nixpkgs.overlays =
69 import nixpkgs/overlays.nix ++
70 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
71 #nixpkgs.buildPlatform = "x86_64-linux";
72 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" ];
73 }
74 inputs.nixpkgs.nixosModules.notDetected
75 inputs.home-manager.nixosModules.home-manager
76 {
77 home-manager.useGlobalPkgs = true;
78 home-manager.useUserPackages = true;
79 home-manager.verbose = true;
80 home-manager.backupFileExtension = null;
81 home-manager.extraSpecialArgs = {
82 inherit hostName inputs;
83 };
84 }
85 {
86 programs.ssh.knownHosts = {
87 carotte = {
88 extraHostNames = [ "carotte" "carotte.sourcephile.fr" ];
89 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
90 };
91 mermet = {
92 extraHostNames = [ "mermet" "mermet.sourcephile.fr" ];
93 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
94 };
95 losurdo = {
96 extraHostNames = [ "losurdo" "losurdo.sourcephile.fr" ];
97 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
98 };
99 };
100 }
101 ];
102 }
103 );
104
105 # nix -L develop or direnv allow
106 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
107 {
108 inherit pkgs inputs system nixpkgsPath;
109 #inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
110 });
111
112 # nix -L run .#mermet.switch
113 apps = forAllSystems ({ pkgs, system, ... }:
114 with builtins;
115 mapAttrs
116 (hostName: { config, ... }:
117 let
118 inherit (config.system) build;
119 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
120 scriptApp = scriptName: ps: script: {
121 type = "app";
122 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
123 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
124 set -eux
125 ${script}
126 '').outPath;
127 };
128 in
129 {
130 # Example: nix run .#mermet.switch
131 "switch" = scriptApp "switch" [ ] (''
132 shopt -s globstar
133
134 chmod -R g-rwx,o-rwx **/*.gpg
135 trap 'git reset **/*.gpg' EXIT
136 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
137
138 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
139 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
140
141 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
142 '' +
143 # TODO: make it a LoadCredentialEncrypted= in nix-daemon.service
144 lib.optionalString config.boot.initrd.network.ssh.enable ''
145 # Send the SSH key of the initrd
146 gpg --decrypt 'hosts/${hostName}/ssh/initrd.key.gpg' |
147 ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
148 '' + ''
149 ssh ${target} set -x ';' \
150 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
151 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
152 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
153 sleep $((10 * 60))
154 ${profile}/bin/switch-to-configuration switch
155 systemctl reboot
156 '\'''' '&&' \
157 ${build.toplevel}/bin/switch-to-configuration test
158
159 ssh ${target} -o ControlPath=none set -x ';' \
160 systemctl stop nixos-fallback.service ';' \
161 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
162 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
163 nix-env --delete-generations 7d --profile ${profile}
164 '');
165 # Example: nix run .#carotte.install-sd
166 # DELETEME: quite useless, better to use nixos-install
167 "install-sd" = scriptApp "install-sd" [ zstd ] ''
168 set -o pipefail
169 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
170 unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
171 sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
172 '';
173 }
174 )
175 inputs.self.nixosConfigurations
176 );
177 };
178 }