]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
Maintenance/Analysability(nix): remove unused flake inputs
[julm/julm-nix.git] / flake.nix
1 {
2 description = "julm's Nix configurations for hosts (NixOS) and homes (home-manager)";
3 /*
4 nixConfig = {
5 extra-substituters = [
6 "https://nix-community.cachix.org"
7 ];
8 extra-trusted-public-keys = [
9 "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
10 ];
11 };
12 */
13
14 inputs = {
15 /*
16 doom-emacs.flake = false;
17 doom-emacs.url = "github:hlissner/doom-emacs";
18 */
19 jj-nvim = {
20 url = "github:NicolasGB/jj.nvim";
21 flake = false;
22 };
23 git-hooks.inputs.nixpkgs.follows = "nixpkgs";
24 git-hooks.url = "github:cachix/git-hooks.nix";
25 home-manager.inputs.nixpkgs.follows = "nixpkgs";
26 home-manager.url = "github:nix-community/home-manager/master";
27 #home-manager.url = "git+file:///home/julm/src/nix/home-manager";
28 nixos-hardware.url = "github:NixOS/nixos-hardware/master";
29 #nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
30 nixpkgs.url = "github:NixOS/nixpkgs/9cb344e96d5b6918e94e1bca2d9f3ea1e9615545";
31 nixpkgs-unstable.url = "github:NixOS/nixpkgs/master";
32 nixpkgs-unstable-latest.url = "github:NixOS/nixpkgs/cb82756ecc37fa623f8cf3e88854f9bf7f64af93";
33 lanzaboote = {
34 url = "github:nix-community/lanzaboote/v0.4.2";
35 inputs.nixpkgs.follows = "nixpkgs";
36 inputs.pre-commit-hooks-nix.follows = "git-hooks";
37 };
38 };
39
40 outputs =
41 inputs:
42 let
43 remoteNixpkgsPatches = import nixpkgs/patches.nix;
44 localNixpkgsPatches = [
45 nixpkgs/patches/openvpn/0001-nixos-netns-init-module-to-manage-network-namespaces.patch
46 nixpkgs/patches/openvpn/0002-nixos-openvpn-add-netns-support.patch
47 ];
48 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
49 nixpkgsPath = originPkgs.applyPatches {
50 name = "nixpkgs-patched";
51 src = inputs.nixpkgs.outPath;
52 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
53 postPatch = ''
54 patch=$(printf '%s\n' ${
55 builtins.concatStringsSep " " (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)
56 } |
57 sort | sha256sum | cut -c -7)
58 echo "-patch-$patch" >.version-suffix
59 '';
60 };
61 profile = "/nix/var/nix/profiles/system";
62 inherit (inputs.nixpkgs) lib;
63 overlays = _system: import nixpkgs/overlays.nix;
64 #nixosSystem = lib.nixosSystem;
65 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
66 forAllSystems =
67 f:
68 lib.genAttrs lib.systems.flakeExposed (
69 system:
70 f rec {
71 inherit system;
72 #pkgs = inputs.nixpkgs.legacyPackages.${system};
73 pkgs = import nixpkgsPath {
74 inherit system;
75 overlays = overlays system;
76 };
77 }
78 );
79 self =
80 with lib.fileset;
81 toSource {
82 root = ./.;
83 fileset =
84 let
85 exts = [
86 "clear"
87 "conf"
88 "cred"
89 "crt"
90 "css"
91 "el"
92 "hs"
93 "inc"
94 "json"
95 "lua"
96 "nix"
97 "patch"
98 "pem"
99 "pub"
100 "sh"
101 "theme"
102 "vim"
103 ];
104 in
105 unions [
106 (fileFilter (file: lib.any file.hasExt exts) ./domains)
107 (fileFilter (file: lib.any file.hasExt exts) ./home-manager)
108 (fileFilter (file: lib.any file.hasExt exts) ./homes)
109 (fileFilter (file: lib.any file.hasExt exts) ./hosts)
110 (fileFilter (file: lib.any file.hasExt exts) ./nixos)
111 (fileFilter (file: lib.any file.hasExt exts) ./nixpkgs)
112 (fileFilter (file: lib.any file.hasExt exts) ./users)
113 ./shell.nix
114 ];
115 };
116 in
117 rec {
118 # nix -L build .#hello
119 packages = forAllSystems ({ pkgs, ... }: pkgs);
120
121 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
122 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
123 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
124 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.ruleset
125 nixosConfigurations =
126 lib.genAttrs
127 (builtins.attrNames (
128 lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (self + "/hosts"))
129 ))
130 (
131 hostName:
132 nixosSystem {
133 system = null;
134 specialArgs = {
135 # Required to avoid infinite recursion
136 # when `inputs` is used in `imports`.
137 inherit inputs;
138 };
139 modules = [
140 nixos/default.nix
141 (import (self + "/hosts/${hostName}.nix"))
142 inputs.lanzaboote.nixosModules.lanzaboote
143 inputs.home-manager.nixosModules.home-manager
144 (
145 { config, ... }:
146 let
147 inherit (config.nixpkgs.hostPlatform) system;
148 pkgs-unstable = import inputs.nixpkgs-unstable.outPath {
149 inherit system;
150 overlays = overlays system;
151 };
152 pkgs-unstable-latest = import inputs.nixpkgs-unstable-latest.outPath {
153 inherit system;
154 overlays = overlays system;
155 };
156 in
157 {
158 _module.args = {
159 inherit hostName pkgs-unstable pkgs-unstable-latest;
160 hosts = nixosConfigurations;
161 host = nixosConfigurations.${hostName}._module.args;
162 };
163 nixpkgs = {
164 overlays = overlays config.nixpkgs.hostPlatform.system;
165 config.permittedInsecurePackages = [
166 # Still needed for chatty
167 # See https://github.com/NixOS/nixpkgs/pull/334638#issuecomment-2289025802%3E
168 # and https://gitlab.gnome.org/World/Chatty/-/issues/932
169 "olm-3.2.16"
170 ];
171 config.allowUnfreePredicate =
172 pkg:
173 builtins.elem (lib.getName pkg) [
174 "anydesk"
175 "canon-cups-ufr2"
176 "cudatoolkit"
177 "geogebra"
178 "hplip"
179 "memtest86-efi"
180 "nvidia-settings"
181 "nvidia-x11"
182 ];
183 };
184 home-manager = {
185 useGlobalPkgs = true;
186 useUserPackages = true;
187 verbose = true;
188 #force = true;
189 backupFileExtension = "old";
190 extraSpecialArgs = {
191 inherit
192 inputs
193 hostName
194 pkgs-unstable
195 pkgs-unstable-latest
196 ;
197 };
198 };
199 }
200 )
201 ];
202 }
203 );
204
205 # nix -L develop or direnv allow
206 devShells = forAllSystems (
207 { pkgs, system, ... }:
208 {
209 default = pkgs.callPackage (self + "/shell.nix") {
210 inherit
211 pkgs
212 inputs
213 system
214 nixpkgsPath
215 ;
216 inherit (checks.${system}.git-hooks-check) shellHook;
217 };
218 }
219 );
220
221 # nix -L run .#oignon.switch
222 apps = forAllSystems (
223 { pkgs, system, ... }:
224 with builtins;
225 mapAttrs (
226 hostName:
227 { config, ... }:
228 let
229 inherit (config.system) build;
230 scriptApp = scriptName: ps: script: {
231 type = "app";
232 program =
233 (pkgs.writeShellScript "${hostName}-${scriptName}" ''
234 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
235 set -eux
236 ${script}
237 '').outPath;
238 };
239 in
240 {
241 # Example: nix run .#aubergine.switch
242 "switch" = scriptApp "switch" [ ] ''
243 shopt -s globstar
244
245 chmod -R g-rwx,o-rwx **/*.gpg
246 trap 'git reset **/*.gpg' EXIT
247 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
248
249 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
250 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
251
252 nix copy --to "ssh://${config.install.target}?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
253
254 if ssh ${config.install.target} set -x ';' \
255 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
256 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
257 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
258 PATH=${
259 with pkgs;
260 lib.makeBinPath [
261 coreutils
262 nix
263 systemd
264 ]
265 }
266 sleep $((10 * 60))
267 ${profile}/bin/switch-to-configuration switch
268 systemctl reboot
269 '\'''' '&&' \
270 ${build.toplevel}/bin/switch-to-configuration test
271 then
272 ssh ${config.install.target} -o ControlPath=none set -x ';' \
273 systemctl stop nixos-fallback.service ';' \
274 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
275 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
276 nix-env --delete-generations 7d --profile ${profile}
277 else
278 tput rev
279 echo WARNING: switch-to-configuration was not registered at boot
280 tput sgr0
281 ssh ${config.install.target} -o ControlPath=none set -x ';' \
282 systemctl stop nixos-fallback.service
283 false
284 fi
285 '';
286 }
287 ) nixosConfigurations
288 );
289
290 # nix flake check
291 checks = forAllSystems (
292 args: with args; {
293 git-hooks-check = inputs.git-hooks.lib.${system}.run {
294 src = self;
295 hooks = {
296 nixfmt-rfc-style.enable = true;
297 };
298 };
299 }
300 );
301 };
302 }