]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nixos-fallback: fix switch
[julm/julm-nix.git] / flake.nix
1 {
2 inputs.nixpkgs.url = "github:NixOS/nixpkgs/32096899af23d49010bd8cf6a91695888d9d9e73";
3 inputs.home-manager.url = "github:nix-community/home-manager";
4 inputs.home-manager.inputs.nixpkgs.follows = "nixpkgs";
5 inputs.doom-emacs.url = "github:hlissner/doom-emacs";
6 inputs.doom-emacs.flake = false;
7 /*
8 inputs.nix-doom-emacs.url = "github:vlaci/nix-doom-emacs";
9 inputs.nix-doom-emacs.inputs.nixpkgs.follows = "nixpkgs";
10 inputs.nix-doom-emacs.inputs.home-manager.follows = "home-manager";
11 inputs.nix-doom-emacs.inputs.flake-utils.follows = "flake-utils";
12 inputs.nix-doom-emacs.inputs.emacs-overlay.follows = "emacs-overlay";
13 inputs.emacs-overlay.url = "github:nix-community/emacs-overlay/master";
14 */
15 #inputs.pass = { type = "path"; path = "./pass"; flake = false; };
16
17 outputs = inputs: let
18 remoteNixpkgsPatches = import nixpkgs/patches.nix;
19 localNixpkgsPatches = [
20 ];
21 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
22 nixpkgsPath = originPkgs.applyPatches {
23 name = "nixpkgs-patched";
24 src = inputs.nixpkgs.outPath;
25 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
26 postPatch = ''
27 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
28 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
29 sort | sha256sum | cut -c -7)
30 echo "-patch-$patch" >.version-suffix
31 '';
32 };
33 lib = inputs.nixpkgs.lib;
34 profile = "/nix/var/nix/profiles/system";
35 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
36 inherit system;
37 #pkgs = inputs.nixpkgs.legacyPackages.${system};
38 pkgs = pkgsForSystem system;
39 });
40 pkgsForSystem = system: import nixpkgsPath {
41 inherit system;
42 config = {
43 allowUnfree = true; # For hplip
44 };
45 overlays = import nixpkgs/overlays.nix;
46 };
47 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
48 #nixosSystem = inputs.nixpkgs.lib.nixosSystem;
49 in {
50 nixosConfigurations = lib.genAttrs
51 (builtins.attrNames (lib.filterAttrs (n: v: v == "directory") (builtins.readDir ./hosts)))
52 (hostName: nixosSystem {
53 system = "x86_64-linux";
54 pkgs = pkgsForSystem "x86_64-linux";
55 specialArgs = {
56 inherit hostName inputs;
57 };
58 modules = [
59 {
60 config._module.args = {
61 inherit hostName inputs;
62 hosts = inputs.self.nixosConfigurations;
63 };
64 }
65 (import nixos/modules.nix)
66 (import nixos/options.nix)
67 (import (./hosts + "/${hostName}.nix"))
68 inputs.nixpkgs.nixosModules.notDetected
69 inputs.home-manager.nixosModules.home-manager
70 ({ pkgs, ... }: {
71 nix.registry.nixpkgs = lib.mkDefault { flake = inputs.nixpkgs; };
72 nix.package = pkgs.nixFlakes;
73 nix.extraOptions = "experimental-features = nix-command flakes";
74 #nixpkgs.overlays = import nixpkgs/overlays.nix;
75 /*
76 system.nixos.versionSuffix = ".${
77 lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${
78 inputs.self.shortRev or "dirty"}";
79 system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
80 */
81 # Let 'nixos-version --json' know about the Git revision of this flake.
82 system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
83 /*
84 system.configurationRevision =
85 if inputs.self ? rev
86 then inputs.self.rev
87 else throw "Refusing to build from a dirty Git tree!";
88 */
89 })
90 {
91 home-manager.useGlobalPkgs = true;
92 home-manager.useUserPackages = true;
93 home-manager.verbose = true;
94 home-manager.backupFileExtension = null;
95 home-manager.extraSpecialArgs = {
96 inherit hostName inputs;
97 };
98 }
99 ];
100 });
101 # nix -L develop or direnv allow
102 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
103 {
104 inherit pkgs inputs system nixpkgsPath;
105 #inherit (checks.${system}.pre-commit-check) shellHook;
106 });
107 # nix -L run .#oignon.switch
108 apps = forAllSystems ({ pkgs, system, ... }:
109 with builtins;
110 mapAttrs (hostName: { config, ... }:
111 let
112 build = config.system.build;
113 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
114 scriptApp = scriptName: ps: script: {
115 type = "app";
116 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
117 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
118 set -eux
119 ${script}
120 '').outPath;
121 };
122 in rec {
123 # Example: nix run .#aubergine.switch
124 "switch" = scriptApp "switch" [] ''
125 shopt -s globstar
126
127 chmod -R g-rwx,o-rwx **/*.gpg
128 trap 'git reset **/*.gpg' EXIT
129 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
130
131 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
132 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
133
134 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
135
136 ssh ${target} set -x ';' \
137 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
138 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
139 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
140 sleep $((10 * 60))
141 ${profile}/bin/switch-to-configuration switch
142 systemctl reboot
143 '\'''' '&&' \
144 ${build.toplevel}/bin/switch-to-configuration test
145
146 ssh ${target} -o ControlPath=none set -x ';' \
147 systemctl stop nixos-fallback.service ';' \
148 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
149 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
150 nix-env --delete-generations 7d --profile ${profile}
151 '';
152 }) inputs.self.nixosConfigurations);
153 };
154 }