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