]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
015fe129a42d11b3759387afc51a6b1e0dceceb4
[julm/julm-nix.git] / flake.nix
1 {
2 /*
3 nixConfig = {
4 extra-substituters = [
5 "https://nix-community.cachix.org"
6 ];
7 extra-trusted-public-keys = [
8 "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
9 ];
10 };
11 */
12
13 inputs = {
14 doom-emacs.flake = false;
15 doom-emacs.url = "github:hlissner/doom-emacs";
16 home-manager.inputs.nixpkgs.follows = "nixpkgs";
17 home-manager.url = "github:nix-community/home-manager/release-23.11";
18 nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
19 nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
20 nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
21 pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
22 pre-commit-hooks.inputs.nixpkgs-stable.follows = "nixpkgs";
23 pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
24 };
25
26 outputs = inputs:
27 let
28 remoteNixpkgsPatches = import nixpkgs/patches.nix;
29 localNixpkgsPatches = [
30 ];
31 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
32 nixpkgsPath = originPkgs.applyPatches {
33 name = "nixpkgs-patched";
34 src = inputs.nixpkgs.outPath;
35 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
36 postPatch = ''
37 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
38 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
39 sort | sha256sum | cut -c -7)
40 echo "-patch-$patch" >.version-suffix
41 '';
42 };
43 profile = "/nix/var/nix/profiles/system";
44 inherit (inputs.nixpkgs) lib;
45 #nixosSystem = lib.nixosSystem;
46 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
47 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
48 inherit system;
49 #pkgs = inputs.nixpkgs.legacyPackages.${system};
50 pkgs = import nixpkgsPath {
51 inherit system;
52 overlays = import nixpkgs/overlays.nix;
53 };
54 });
55 self = with lib.fileset; toSource {
56 root = ./.;
57 fileset =
58 intersection
59 (unions [
60 ./home-manager
61 ./homes
62 ./hosts
63 ./nixos
64 ./nixpkgs
65 ./share
66 ./users
67 ./shell.nix
68 ])
69 (fileFilter
70 (file: lib.any file.hasExt [
71 "clear"
72 "conf"
73 "cred"
74 "crt"
75 "css"
76 "el"
77 "hs"
78 "json"
79 "nix"
80 "patch"
81 "pem"
82 "pub"
83 "sh"
84 "theme"
85 "vim"
86 ]) ./.);
87 };
88 in
89 rec {
90 # nix -L build .#hello
91 packages = forAllSystems ({ pkgs, ... }: pkgs);
92
93 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
94 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
95 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
96 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.ruleset
97 nixosConfigurations = lib.genAttrs
98 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (self + "/hosts"))))
99 (hostName: nixosSystem {
100 system = null;
101 specialArgs = {
102 # Required to avoid infinite recursion
103 # when `inputs` is used in `imports`.
104 inherit inputs;
105 };
106 modules = [
107 nixos/default.nix
108 (import (self + "/hosts/${hostName}.nix"))
109 {
110 _module.args = {
111 inherit hostName;
112 hosts = nixosConfigurations;
113 host = nixosConfigurations.${hostName}._module.args;
114 };
115 nixpkgs.overlays = import (self + "/nixpkgs/overlays.nix");
116 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
117 "anydesk"
118 "cudatoolkit"
119 "geogebra"
120 "hplip"
121 "memtest86-efi"
122 "nvidia-settings"
123 "nvidia-x11"
124 ];
125 }
126 inputs.home-manager.nixosModules.home-manager
127 {
128 home-manager.useGlobalPkgs = true;
129 home-manager.useUserPackages = true;
130 home-manager.verbose = true;
131 home-manager.backupFileExtension = null;
132 home-manager.extraSpecialArgs = {
133 inherit hostName inputs;
134 };
135 }
136 ];
137 });
138
139 # nix -L develop or direnv allow
140 devShells = forAllSystems ({ pkgs, system, ... }:
141 {
142 default = pkgs.callPackage (self + "/shell.nix")
143 {
144 inherit pkgs inputs system nixpkgsPath;
145 inherit (checks.${system}.pre-commit-check) shellHook;
146 };
147 }
148 );
149
150 # nix -L run .#oignon.switch
151 apps = forAllSystems ({ pkgs, system, ... }:
152 with builtins;
153 mapAttrs
154 (hostName: { config, ... }:
155 let
156 inherit (config.system) build;
157 scriptApp = scriptName: ps: script: {
158 type = "app";
159 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
160 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
161 set -eux
162 ${script}
163 '').outPath;
164 };
165 in
166 {
167 # Example: nix run .#aubergine.switch
168 "switch" = scriptApp "switch" [ ] ''
169 shopt -s globstar
170
171 chmod -R g-rwx,o-rwx **/*.gpg
172 trap 'git reset **/*.gpg' EXIT
173 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
174
175 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
176 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
177
178 nix copy --to "ssh://${config.install.target}?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
179
180 if ssh ${config.install.target} set -x ';' \
181 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
182 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
183 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
184 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
185 sleep $((10 * 60))
186 ${profile}/bin/switch-to-configuration switch
187 systemctl reboot
188 '\'''' '&&' \
189 ${build.toplevel}/bin/switch-to-configuration test
190 then
191 ssh ${config.install.target} -o ControlPath=none set -x ';' \
192 systemctl stop nixos-fallback.service ';' \
193 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
194 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
195 nix-env --delete-generations 7d --profile ${profile}
196 else
197 tput rev
198 echo WARNING: switch-to-configuration was not registered at boot
199 tput sgr0
200 ssh ${config.install.target} -o ControlPath=none set -x ';' \
201 systemctl stop nixos-fallback.service
202 false
203 fi
204 '';
205 })
206 nixosConfigurations);
207
208 # nix flake check
209 checks = forAllSystems (args: with args; {
210 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
211 src = self;
212 hooks = {
213 nixpkgs-fmt.enable = true;
214 };
215 };
216 });
217
218 # nix fmt
219 formatter = forAllSystems ({ pkgs, ... }:
220 inputs.nix-formatter-pack.lib.mkFormatter {
221 inherit pkgs;
222 config = {
223 tools = {
224 deadnix.enable = true;
225 nixpkgs-fmt.enable = true;
226 statix.enable = true;
227 };
228 };
229 }
230 );
231 };
232 }