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