about summary refs log tree commit diff
path: root/nix/lib/flake-helper.nix
diff options
context:
space:
mode:
authorEmile <git@emile.space>2024-09-19 10:48:16 +0200
committerEmile <git@emile.space>2024-09-19 10:48:16 +0200
commite833c13c177288b2a7b9168a784e0e6562c025b6 (patch)
tree1661dad1694989e2d83e6c2fca7b5602a61c574b /nix/lib/flake-helper.nix
parentdcde3882d5460368e3c0499a54330f84a6995d62 (diff)
big format using nixfmt
Diffstat (limited to 'nix/lib/flake-helper.nix')
-rw-r--r--nix/lib/flake-helper.nix382
1 files changed, 212 insertions, 170 deletions
diff --git a/nix/lib/flake-helper.nix b/nix/lib/flake-helper.nix
index b720232..ed5e9ba 100644
--- a/nix/lib/flake-helper.nix
+++ b/nix/lib/flake-helper.nix
@@ -1,173 +1,215 @@
-{ self, agenix, nixpkgs, nixpkgs-unstable, deploy-rs, home-manager, darwin, ... }@inputs:
+{
+  self,
+  agenix,
+  nixpkgs,
+  nixpkgs-unstable,
+  deploy-rs,
+  home-manager,
+  darwin,
+  ...
+}@inputs:
 
 rec {
-	generateSystem = name: {
-		hostname ? name,
-		username ? "emile",
-		system ? "x86_64-linux",
-		deployUser ? "root",
-		homeManagerEnable ? false,
-		group ? null,
-		modules ? [],
-		...
-	}:
-	let
-
-		# inputs.nixpkgs-${name}, if that doesn't exist, just use nixpkgs
-		localNixpkgs =
-			nixpkgs.lib.attrByPath
-				[ "nixpkgs-${name}" ] # path
-				nixpkgs # default
-				inputs; # base
-
-		# determine if our system type that is used further down
-		systemType =
-			if system == "x86_64-linux" then localNixpkgs.lib.nixosSystem
-			else
-				if system == "aarch64-darwin" then darwin.lib.darwinSystem
-				else null;
-		
-	in systemType { # this may fail if we aren't using x86_64-linux or aarch64-darwin
-		inherit system;
-
-		# ; nix repl
-		# nix-repl> :lf .
-		# nix-repl> nixosConfigurations.corrino._module.args.modules
-
-		modules = modules ++ [
-
-			(if system == "x86_64-linux" then self.nixosModules.x86_64-linux
-				else
-					if system == "aarch64-darwin" then ({})
-					else null)
-
-			# a module so that we can access the flake output from inside the
-			# flake (yes, I need this for fetching the system type while building the hosts for deploy-rs)
-			{ config._module.args = { flake = self; }; }
-
-			# overlays
-			({ ... }: {
-				nixpkgs.overlays = [
-					self.overlays.emile
-					
-					(_: _: { inherit (agenix.packages."x86_64-linux") agenix; })
-					(_: _: {
-						unstable = import nixpkgs-unstable {
-							system = "x86_64-linux";
-							config.allowUnfree = true;
-						};
-					})
-				];
-			})
-
-			# general modules
-			agenix.nixosModules.default
-
-			# # the host config itself
-			(../hosts +
-				(if (system == "x86_64-linux")
-				 then "/${name}/configuration.nix"
-				 else
-					if (system == "aarch64-darwin")
-					then "/${name}/darwin-configuration.nix"
-					else ""))
-
-			# secrets (have to be added to git (crypted) #lessonslearned)
-			({ lib ? (import <nixpkgs/lib>), ... }: let
-				secretsPath = (../hosts + "/${name}/secrets");
-			in {
-				age.secrets = lib.mapAttrs'
-					(filename: _:
-						lib.nameValuePair (lib.removeSuffix ".age" filename)
-						{ file = secretsPath + "/${filename}"; }
-					)
-					(lib.filterAttrs
-						(name: type:
-							(type == "regular") &&
-							(lib.hasSuffix ".age" name) )
-						(if builtins.pathExists secretsPath
-						 then builtins.readDir secretsPath
-						 else {} )
-					);
-			})
-		]
-		
-		++ (if (system == "aarch64-darwin")
-			then [ (home-manager.darwinModules.home-manager) ]
-			else [])
-			
-		++ (if (homeManagerEnable == true)
-			then [{
-				home-manager = {
-					useGlobalPkgs = true;
-					users."${username}" =
-						import (../hosts + "/${hostname}/home_${username}.nix");
-				};
-			}]
-			else []);
-	};
-
-	mapToNixosConfigurations = { system ? "x86_64-linux", ... }@hosts:
-		builtins.mapAttrs
-		(name: host: generateSystem name host)
-		(nixpkgs.lib.filterAttrs
-			(n: v: v.system or "" == "x86_64-linux") hosts);
-
-	mapToDarwinConfigurations = hosts:
-		builtins.mapAttrs
-		(name: host: generateSystem name host)
-		(nixpkgs.lib.filterAttrs
-			(n: v: v.system or "" == "aarch64-darwin") hosts);
-
-	generateDeployRsHost = name: {
-		hostname ? name,
-		ip ? "${name}.pinto-pike.ts.net",
-		sshUser ? "root",
-		system ? "x86_64-linux",
-		...
-	}: {
-		reboteBuild = true;
-		hostname = "${ip}";
-		fastConnection = true;
-		profiles.system = {
-			user = "root"; # user to install as
-			sshUser = sshUser; # user to ssh to as
-
-			# make sure people can use sudo 
-			# sshOpts = ["-A", "-t", "-S"];
-
-			# make sure to add the nix foo on the darwin hosts to ~/.zshenv
-			# as the ~/.zshrc doesn't get sourced when ssh-ing into the system
-
-			path = (if system == "x86_64-linux"
-				 then deploy-rs.lib.x86_64-linux.activate.nixos
-					self.nixosConfigurations."${name}"
-				 else
-					if system == "aarch64-darwin"
-					then deploy-rs.lib.aarch64-darwin.activate.darwin
-						self.darwinConfigurations."${name}"
-					else "");
-
-		  # Timeout for profile activation.
-		  # This defaults to 240 seconds.
-		  activationTimeout = 600;
-
-		  # Timeout for profile activation confirmation.
-		  # This defaults to 30 seconds.
-		  confirmTimeout = 120;
-
-		};
-	};
-
-	mapToDeployRsConfiguration = hosts:
-		builtins.mapAttrs (name: host: generateDeployRsHost name host) hosts;
-	
-	buildHosts = hosts:
-		builtins.mapAttrs (name: host: host.config.system.build.toplevel)
-
-		# don't build hosts that start with an underscore
-		(nixpkgs.lib.filterAttrs
-			(name: host: (builtins.substring 0 1 name) != "_")
-			hosts
-		);
+  generateSystem =
+    name:
+    {
+      hostname ? name,
+      username ? "emile",
+      system ? "x86_64-linux",
+      deployUser ? "root",
+      homeManagerEnable ? false,
+      group ? null,
+      modules ? [ ],
+      ...
+    }:
+    let
+
+      # inputs.nixpkgs-${name}, if that doesn't exist, just use nixpkgs
+      localNixpkgs =
+        nixpkgs.lib.attrByPath [ "nixpkgs-${name}" ] # path
+          nixpkgs # default
+          inputs; # base
+
+      # determine if our system type that is used further down
+      systemType =
+        if system == "x86_64-linux" then
+          localNixpkgs.lib.nixosSystem
+        else if system == "aarch64-darwin" then
+          darwin.lib.darwinSystem
+        else
+          null;
+
+    in
+    systemType {
+      # this may fail if we aren't using x86_64-linux or aarch64-darwin
+      inherit system;
+
+      # ; nix repl
+      # nix-repl> :lf .
+      # nix-repl> nixosConfigurations.corrino._module.args.modules
+
+      modules =
+        modules
+        ++ [
+
+          (
+            if system == "x86_64-linux" then
+              self.nixosModules.x86_64-linux
+            else if system == "aarch64-darwin" then
+              ({ })
+            else
+              null
+          )
+
+          # a module so that we can access the flake output from inside the
+          # flake (yes, I need this for fetching the system type while building the hosts for deploy-rs)
+          {
+            config._module.args = {
+              flake = self;
+            };
+          }
+
+          # overlays
+          (
+            { ... }:
+            {
+              nixpkgs.overlays = [
+                self.overlays.emile
+
+                (_: _: { inherit (agenix.packages."x86_64-linux") agenix; })
+                (_: _: {
+                  unstable = import nixpkgs-unstable {
+                    system = "x86_64-linux";
+                    config.allowUnfree = true;
+                  };
+                })
+              ];
+            }
+          )
+
+          # general modules
+          agenix.nixosModules.default
+
+          # # the host config itself
+          (
+            ../hosts
+            + (
+              if (system == "x86_64-linux") then
+                "/${name}/configuration.nix"
+              else if (system == "aarch64-darwin") then
+                "/${name}/darwin-configuration.nix"
+              else
+                ""
+            )
+          )
+
+          # secrets (have to be added to git (crypted) #lessonslearned)
+          (
+            {
+              lib ? (import <nixpkgs/lib>),
+              ...
+            }:
+            let
+              secretsPath = (../hosts + "/${name}/secrets");
+            in
+            {
+              age.secrets =
+                lib.mapAttrs'
+                  (
+                    filename: _:
+                    lib.nameValuePair (lib.removeSuffix ".age" filename) { file = secretsPath + "/${filename}"; }
+                  )
+                  (
+                    lib.filterAttrs (name: type: (type == "regular") && (lib.hasSuffix ".age" name)) (
+                      if builtins.pathExists secretsPath then builtins.readDir secretsPath else { }
+                    )
+                  );
+            }
+          )
+        ]
+
+        ++ (if (system == "aarch64-darwin") then [ (home-manager.darwinModules.home-manager) ] else [ ])
+
+        ++ (
+          if (homeManagerEnable == true) then
+            [
+              {
+                home-manager = {
+                  useGlobalPkgs = true;
+                  users."${username}" = import (../hosts + "/${hostname}/home_${username}.nix");
+                };
+              }
+            ]
+          else
+            [ ]
+        );
+    };
+
+  mapToNixosConfigurations =
+    {
+      system ? "x86_64-linux",
+      ...
+    }@hosts:
+    builtins.mapAttrs (name: host: generateSystem name host) (
+      nixpkgs.lib.filterAttrs (n: v: v.system or "" == "x86_64-linux") hosts
+    );
+
+  mapToDarwinConfigurations =
+    hosts:
+    builtins.mapAttrs (name: host: generateSystem name host) (
+      nixpkgs.lib.filterAttrs (n: v: v.system or "" == "aarch64-darwin") hosts
+    );
+
+  generateDeployRsHost =
+    name:
+    {
+      hostname ? name,
+      ip ? "${name}.pinto-pike.ts.net",
+      sshUser ? "root",
+      system ? "x86_64-linux",
+      ...
+    }:
+    {
+      reboteBuild = true;
+      hostname = "${ip}";
+      fastConnection = true;
+      profiles.system = {
+        user = "root"; # user to install as
+        sshUser = sshUser; # user to ssh to as
+
+        # make sure people can use sudo 
+        # sshOpts = ["-A", "-t", "-S"];
+
+        # make sure to add the nix foo on the darwin hosts to ~/.zshenv
+        # as the ~/.zshrc doesn't get sourced when ssh-ing into the system
+
+        path = (
+          if system == "x86_64-linux" then
+            deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations."${name}"
+          else if system == "aarch64-darwin" then
+            deploy-rs.lib.aarch64-darwin.activate.darwin self.darwinConfigurations."${name}"
+          else
+            ""
+        );
+
+        # Timeout for profile activation.
+        # This defaults to 240 seconds.
+        activationTimeout = 600;
+
+        # Timeout for profile activation confirmation.
+        # This defaults to 30 seconds.
+        confirmTimeout = 120;
+
+      };
+    };
+
+  mapToDeployRsConfiguration =
+    hosts: builtins.mapAttrs (name: host: generateDeployRsHost name host) hosts;
+
+  buildHosts =
+    hosts:
+    builtins.mapAttrs (name: host: host.config.system.build.toplevel)
+
+      # don't build hosts that start with an underscore
+      (nixpkgs.lib.filterAttrs (name: host: (builtins.substring 0 1 name) != "_") hosts);
 }