aboutsummaryrefslogtreecommitdiff
path: root/lib/nix-furnace
diff options
context:
space:
mode:
authorCollin Williams <96917990+bluedragon1221@users.noreply.github.com>2025-06-09 17:40:46 +0000
committerCollin Williams <96917990+bluedragon1221@users.noreply.github.com>2025-06-09 17:40:46 +0000
commita45c4551712c1ba5cb82eaf290f311ab0e8690e4 (patch)
treef62b23489fb31f8ea58fef77f99619efd784eb36 /lib/nix-furnace
big changes
Diffstat (limited to 'lib/nix-furnace')
-rw-r--r--lib/nix-furnace/README.md7
-rw-r--r--lib/nix-furnace/default.nix121
2 files changed, 128 insertions, 0 deletions
diff --git a/lib/nix-furnace/README.md b/lib/nix-furnace/README.md
new file mode 100644
index 0000000..cff5c2d
--- /dev/null
+++ b/lib/nix-furnace/README.md
@@ -0,0 +1,7 @@
+# `nix-furnace`
+my homebrewed library for defining nix machines
+
+Features:
+- Homogenous modules: each module contains a home part, a system part, and a shared `options.nix`
+- Path-based: All modules in `modules/` are imported automatically
+- Exclusive: Only supports single-user nixos machines running home-manager
diff --git a/lib/nix-furnace/default.nix b/lib/nix-furnace/default.nix
new file mode 100644
index 0000000..7c18ae9
--- /dev/null
+++ b/lib/nix-furnace/default.nix
@@ -0,0 +1,121 @@
+let
+ lazyImport = path:
+ if (builtins.pathExists path)
+ then import path
+ else {};
+
+ mkIfNull = cond: onTrue:
+ if cond
+ then onTrue
+ else null;
+
+ getSubdirs = dir:
+ builtins.filter (x: x != null)
+ (builtins.attrValues
+ (builtins.mapAttrs
+ (name: value: mkIfNull (value == "directory") name)
+ (builtins.readDir dir)));
+
+ getSubfiles = dir:
+ builtins.filter (x: x != null)
+ (builtins.attrValues
+ (builtins.mapAttrs
+ (name: value: mkIfNull (value == "file") name)
+ (builtins.readDir dir)));
+
+ mkHomeImports = modName: [
+ (lazyImport ../../modules/${modName}/options.nix)
+ (lazyImport ../../modules/${modName}/home/default.nix)
+ ];
+ mkSystemImports = modName: [
+ (lazyImport ../../modules/${modName}/options.nix)
+ (lazyImport ../../modules/${modName}/system/default.nix)
+ ];
+
+ mkModules = mapFn: {
+ imports =
+ [../../modules/options.nix]
+ ++ (builtins.foldl' (a: b: a ++ b) [] (builtins.map mapFn (getSubdirs ../../modules)));
+ };
+
+ mkHomeModules = mkModules mkHomeImports;
+ mkSystemModules = mkModules mkSystemImports;
+
+ mkHost = {
+ hostname,
+ namespace,
+ inputs,
+ ...
+ }: let
+ preloadedConfig = import ../../hosts/${hostname}/config.nix {inherit inputs;};
+ username = preloadedConfig."${namespace}".user.name;
+
+ furnaceOptions = {lib, ...}: let
+ inherit (lib) mkOption types;
+ in {
+ options.nix-furnace = {
+ extraHomeModules = mkOption {
+ type = with types; listOf path;
+ default = [];
+ };
+
+ extraSystemModules = mkOption {
+ type = with types; listOf path;
+ default = [];
+ };
+ };
+ };
+ in
+ inputs.nixpkgs.lib.nixosSystem {
+ specialArgs = {inherit inputs;};
+ modules =
+ [
+ {networking.hostName = hostname;}
+
+ ../../hosts/${hostname}/config.nix
+
+ mkSystemModules
+
+ furnaceOptions
+
+ # Home-manager
+ inputs.home-manager.nixosModules.home-manager
+ {
+ home-manager = {
+ backupFileExtension = "bak";
+ useGlobalPkgs = true;
+ useUserPackages = true;
+ extraSpecialArgs = {inherit inputs;};
+ users."${username}" = {
+ imports =
+ [
+ mkHomeModules
+
+ furnaceOptions
+
+ ../../hosts/${hostname}/config.nix
+ ]
+ ++ preloadedConfig.nix-furnace.extraHomeModules;
+ };
+ };
+ }
+ ]
+ ++ preloadedConfig.nix-furnace.extraSystemModules;
+ };
+
+ mkFlake = {
+ namespace,
+ inputs,
+ ...
+ }: {
+ nixosConfigurations =
+ builtins.listToAttrs
+ (builtins.map (hostname: {
+ name = hostname;
+ value = mkHost {inherit inputs hostname namespace;};
+ })
+ (getSubdirs ../../hosts));
+ };
+in {
+ inherit mkFlake;
+}