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