]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
pass: enable extensions
[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 let exts = [ "clear" "conf" "cred" "crt" "css" "el" "hs" "json" "nix" "patch" "pem" "pub" "sh" "theme" "vim" ]; in
59 unions [
60 (fileFilter (file: lib.any file.hasExt exts) ./home-manager)
61 (fileFilter (file: lib.any file.hasExt exts) ./homes)
62 (fileFilter (file: lib.any file.hasExt exts) ./hosts)
63 (fileFilter (file: lib.any file.hasExt exts) ./nixos)
64 (fileFilter (file: lib.any file.hasExt exts) ./nixpkgs)
65 (fileFilter (file: lib.any file.hasExt exts) ./share)
66 (fileFilter (file: lib.any file.hasExt exts) ./users)
67 ./shell.nix
68 ];
69 };
70 in
71 rec {
72 # nix -L build .#hello
73 packages = forAllSystems ({ pkgs, ... }: pkgs);
74
75 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
76 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
77 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
78 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.ruleset
79 nixosConfigurations = lib.genAttrs
80 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (self + "/hosts"))))
81 (hostName: nixosSystem {
82 system = null;
83 specialArgs = {
84 # Required to avoid infinite recursion
85 # when `inputs` is used in `imports`.
86 inherit inputs;
87 };
88 modules = [
89 nixos/default.nix
90 (import (self + "/hosts/${hostName}.nix"))
91 {
92 _module.args = {
93 inherit hostName;
94 hosts = nixosConfigurations;
95 host = nixosConfigurations.${hostName}._module.args;
96 };
97 nixpkgs.overlays = import (self + "/nixpkgs/overlays.nix");
98 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
99 "anydesk"
100 "cudatoolkit"
101 "geogebra"
102 "hplip"
103 "memtest86-efi"
104 "nvidia-settings"
105 "nvidia-x11"
106 ];
107 }
108 inputs.home-manager.nixosModules.home-manager
109 {
110 home-manager.useGlobalPkgs = true;
111 home-manager.useUserPackages = true;
112 home-manager.verbose = true;
113 home-manager.backupFileExtension = null;
114 home-manager.extraSpecialArgs = {
115 inherit hostName inputs;
116 };
117 }
118 ];
119 });
120
121 # nix -L develop or direnv allow
122 devShells = forAllSystems ({ pkgs, system, ... }:
123 {
124 default = pkgs.callPackage (self + "/shell.nix")
125 {
126 inherit pkgs inputs system nixpkgsPath;
127 inherit (checks.${system}.pre-commit-check) shellHook;
128 };
129 }
130 );
131
132 # nix -L run .#oignon.switch
133 apps = forAllSystems ({ pkgs, system, ... }:
134 with builtins;
135 mapAttrs
136 (hostName: { config, ... }:
137 let
138 inherit (config.system) build;
139 scriptApp = scriptName: ps: script: {
140 type = "app";
141 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
142 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
143 set -eux
144 ${script}
145 '').outPath;
146 };
147 in
148 {
149 # Example: nix run .#aubergine.switch
150 "switch" = scriptApp "switch" [ ] ''
151 shopt -s globstar
152
153 chmod -R g-rwx,o-rwx **/*.gpg
154 trap 'git reset **/*.gpg' EXIT
155 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
156
157 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
158 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
159
160 nix copy --to "ssh://${config.install.target}?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
161
162 if ssh ${config.install.target} set -x ';' \
163 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
164 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
165 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
166 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
167 sleep $((10 * 60))
168 ${profile}/bin/switch-to-configuration switch
169 systemctl reboot
170 '\'''' '&&' \
171 ${build.toplevel}/bin/switch-to-configuration test
172 then
173 ssh ${config.install.target} -o ControlPath=none set -x ';' \
174 systemctl stop nixos-fallback.service ';' \
175 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
176 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
177 nix-env --delete-generations 7d --profile ${profile}
178 else
179 tput rev
180 echo WARNING: switch-to-configuration was not registered at boot
181 tput sgr0
182 ssh ${config.install.target} -o ControlPath=none set -x ';' \
183 systemctl stop nixos-fallback.service
184 false
185 fi
186 '';
187 })
188 nixosConfigurations);
189
190 # nix flake check
191 checks = forAllSystems (args: with args; {
192 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
193 src = self;
194 hooks = {
195 nixpkgs-fmt.enable = true;
196 };
197 };
198 });
199
200 # nix fmt
201 formatter = forAllSystems ({ pkgs, ... }:
202 inputs.nix-formatter-pack.lib.mkFormatter {
203 inherit pkgs;
204 config = {
205 tools = {
206 deadnix.enable = true;
207 nixpkgs-fmt.enable = true;
208 statix.enable = true;
209 };
210 };
211 }
212 );
213 };
214 }