aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/cfgWrapper.nix63
-rw-r--r--lib/nix-furnace/README.md7
-rw-r--r--lib/nix-furnace/default.nix121
3 files changed, 191 insertions, 0 deletions
diff --git a/lib/cfgWrapper.nix b/lib/cfgWrapper.nix
new file mode 100644
index 0000000..2e57656
--- /dev/null
+++ b/lib/cfgWrapper.nix
@@ -0,0 +1,63 @@
+{pkgs}: {
+ pkg,
+ binName ? pkg.meta.mainProgram,
+ extraFlags ? [],
+ extraWrapperFlags ? [],
+ extraPkgs ? [],
+ extraEnv ? {},
+ postBuild ? "",
+ hidePkgs ? false,
+ reducePath ? false,
+}: let
+ extraFlagsArgs = pkgs.lib.pipe extraFlags [
+ (builtins.map (x: ''--add-flags "${x}" ''))
+ (builtins.concatStringsSep " ")
+ ];
+
+ extraWrapperFlagsArgs = builtins.concatStringsSep " " extraWrapperFlags;
+
+ pathArg =
+ if (extraPkgs != [])
+ then ''--prefix PATH : "${pkgs.lib.makeBinPath extraPkgs}"''
+ else "";
+
+ # Looks like {
+ # VAR = "value";
+ # VAR2 = "value_again";
+ # }
+ envArgs = pkgs.lib.pipe extraEnv [
+ (builtins.mapAttrs (k: v: ''--set ${k} "${v}"''))
+ builtins.attrValues
+ (builtins.concatStringsSep " ")
+ ];
+
+ extraPkgs2 =
+ if reducePath
+ then
+ (pkgs.symlinkJoin {
+ name = "${binName}-extra-pkgs";
+ paths = extraPkgs;
+ })
+ else extraPkgs;
+
+ wrapped-program = pkgs.symlinkJoin {
+ name = "${binName}-wrapped-program";
+ paths = [pkg] ++ extraPkgs2;
+
+ buildInputs = [pkgs.makeWrapper];
+ postBuild = ''
+ ${postBuild}
+
+ wrapProgram $out/bin/${binName} ${extraFlagsArgs} ${pathArg} ${envArgs} ${extraWrapperFlagsArgs}
+ '';
+ };
+in
+ if hidePkgs
+ then
+ (pkgs.linkFarm "${binName}-only-bin" [
+ {
+ name = "bin/${binName}";
+ path = "${wrapped-program}/bin/${binName}";
+ }
+ ])
+ else wrapped-program
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;
+}