]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nix: cleanup
[julm/julm-nix.git] / flake.nix
1 {
2 inputs = {
3 doom-emacs.flake = false;
4 doom-emacs.url = "github:hlissner/doom-emacs";
5 home-manager.inputs.nixpkgs.follows = "nixpkgs";
6 home-manager.url = "github:nix-community/home-manager";
7 nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
8 nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
9 nixpkgs.url = "github:NixOS/nixpkgs/fdebb81f45a1ba2c4afca5fd9f526e1653ad0949";
10 pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
11 pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
12 };
13
14 outputs = inputs:
15 let
16 remoteNixpkgsPatches = import nixpkgs/patches.nix;
17 localNixpkgsPatches = [
18 ];
19 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
20 nixpkgsPath = originPkgs.applyPatches {
21 name = "nixpkgs-patched";
22 src = inputs.nixpkgs.outPath;
23 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
24 postPatch = ''
25 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
26 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
27 sort | sha256sum | cut -c -7)
28 echo "-patch-$patch" >.version-suffix
29 '';
30 };
31 profile = "/nix/var/nix/profiles/system";
32 inherit (inputs.nixpkgs) lib;
33 #nixosSystem = lib.nixosSystem;
34 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
35 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
36 inherit system;
37 #pkgs = inputs.nixpkgs.legacyPackages.${system};
38 pkgs = import nixpkgsPath {
39 inherit system;
40 overlays = import nixpkgs/overlays.nix;
41 };
42 });
43 in
44 {
45 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
46 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
47 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
48 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.rulese
49 nixosConfigurations = lib.genAttrs
50 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
51 (hostName: nixosSystem {
52 system = null;
53 specialArgs = {
54 # Required to avoid infinite recursion
55 # when `inputs` is used in `imports`.
56 inherit inputs;
57 };
58 modules = [
59 nixos/default.nix
60 (import (./hosts + "/${hostName}.nix"))
61 {
62 _module.args = {
63 inherit hostName;
64 hosts = inputs.self.nixosConfigurations;
65 host = inputs.self.nixosConfigurations.${hostName}._module.args;
66 };
67 nixpkgs.overlays = import nixpkgs/overlays.nix;
68 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" "geogebra" ];
69 }
70 inputs.home-manager.nixosModules.home-manager
71 {
72 home-manager.useGlobalPkgs = true;
73 home-manager.useUserPackages = true;
74 home-manager.verbose = true;
75 home-manager.backupFileExtension = null;
76 home-manager.extraSpecialArgs = {
77 inherit hostName inputs;
78 };
79 }
80 ];
81 });
82
83 # nix -L develop or direnv allow
84 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
85 {
86 inherit pkgs inputs system nixpkgsPath;
87 inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
88 });
89
90 # nix -L run .#oignon.switch
91 apps = forAllSystems ({ pkgs, system, ... }:
92 with builtins;
93 mapAttrs
94 (hostName: { config, ... }:
95 let
96 inherit (config.system) build;
97 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
98 scriptApp = scriptName: ps: script: {
99 type = "app";
100 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
101 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
102 set -eux
103 ${script}
104 '').outPath;
105 };
106 in
107 {
108 # Example: nix run .#aubergine.switch
109 "switch" = scriptApp "switch" [ ] ''
110 shopt -s globstar
111
112 chmod -R g-rwx,o-rwx **/*.gpg
113 trap 'git reset **/*.gpg' EXIT
114 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
115
116 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
117 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
118
119 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
120
121 ssh ${target} set -x ';' \
122 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
123 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
124 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
125 sleep $((10 * 60))
126 ${profile}/bin/switch-to-configuration switch
127 systemctl reboot
128 '\'''' '&&' \
129 ${build.toplevel}/bin/switch-to-configuration test
130
131 ssh ${target} -o ControlPath=none set -x ';' \
132 systemctl stop nixos-fallback.service ';' \
133 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
134 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
135 nix-env --delete-generations 7d --profile ${profile}
136 '';
137 })
138 inputs.self.nixosConfigurations);
139
140 # nix flake check
141 checks = forAllSystems (args: with args; {
142 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
143 src = inputs.self;
144 hooks = {
145 nixpkgs-fmt.enable = true;
146 };
147 };
148 });
149
150 # nix fmt
151 formatter = forAllSystems ({ pkgs, ... }:
152 inputs.nix-formatter-pack.lib.mkFormatter {
153 inherit pkgs;
154 config = {
155 tools = {
156 deadnix.enable = true;
157 nixpkgs-fmt.enable = true;
158 statix.enable = true;
159 };
160 };
161 }
162 );
163 };
164 }