1
Fork 0
mirror of https://github.com/RGBCube/hjem synced 2025-10-13 13:12:16 +00:00

lib: liberate from the module system (fixes #52)

This commit is contained in:
éclairevoyant 2025-08-24 08:27:25 -04:00
parent 565a15366d
commit e026bdfcf6
No known key found for this signature in database
GPG key ID: E3813AEAA02DB54B
4 changed files with 159 additions and 135 deletions

View file

@ -21,7 +21,19 @@
forAllSystems = nixpkgs.lib.genAttrs ["x86_64-linux" "aarch64-linux"];
in {
nixosModules = {
hjem = ./modules/nixos;
hjem = {
imports = [
self.nixosModules.hjem-lib
./modules/nixos
];
};
hjem-lib = {
lib,
pkgs,
...
}: {
_module.args.hjem-lib = import ./lib.nix {inherit lib pkgs;};
};
default = self.nixosModules.hjem;
};
@ -85,5 +97,11 @@
'';
}
);
hjem-lib = forAllSystems (system:
import ./lib.nix {
inherit (nixpkgs) lib;
pkgs = nixpkgs.legacyPackages.${system};
});
};
}

22
lib.nix
View file

@ -1,21 +1,22 @@
{
config,
lib,
osOptions,
pkgs,
...
}: let
inherit (builtins) isList;
inherit (lib.modules) mkDefault mkDerivedConfig mkIf mkMerge;
inherit (lib.options) literalExpression mkEnableOption mkOption;
inherit (lib.strings) concatMapStringsSep hasPrefix;
inherit (lib.types) addCheck anything attrsOf bool either functionTo lines nullOr path str submodule;
cfg = config;
inherit (lib.types) addCheck anything attrsOf bool either functionTo int lines listOf nullOr oneOf path str submodule;
in {
_module.args.hjem = {
envVarType = osOptions.environment.variables.type;
# inlined from https://github.com/NixOS/nixpkgs/tree/master/nixos/modules/config/shells-environment.nix
# using osOptions precludes using hjem (or this type) standalone
envVarType = attrsOf (nullOr (oneOf [(listOf (oneOf [int str path])) int str path]));
fileTypeRelativeTo = rootDir:
fileTypeRelativeTo = {
rootDir,
clobberDefault,
clobberDefaultText,
}:
submodule ({
name,
target,
@ -92,8 +93,8 @@ in {
clobber = mkOption {
type = bool;
default = cfg.clobberFiles;
defaultText = literalExpression "config.hjem.clobberByDefault";
default = clobberDefault;
defaultText = clobberDefaultText;
description = ''
Whether to "clobber" existing target paths.
@ -143,5 +144,4 @@ in {
if isList env
then concatMapStringsSep ":" toString env
else toString env;
};
}

View file

@ -4,13 +4,14 @@
# be avoided here.
{
config,
hjem,
hjem-lib,
lib,
name,
options,
pkgs,
...
}: let
inherit (hjem) envVarType fileTypeRelativeTo toEnv;
inherit (hjem-lib) envVarType toEnv;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.strings) concatLines;
inherit (lib.modules) mkIf;
@ -18,13 +19,17 @@
inherit (lib.types) attrsOf bool listOf package path str;
cfg = config;
fileTypeRelativeTo' = rootDir:
hjem-lib.fileTypeRelativeTo {
inherit rootDir;
clobberDefault = cfg.clobberFiles;
clobberDefaultText = literalExpression "config.hjem.users.${name}.clobberFiles";
};
in {
imports = [
# Makes "assertions" option available without having to duplicate the work
# already done in the Nixpkgs module.
(pkgs.path + "/nixos/modules/misc/assertions.nix")
../../lib.nix
];
options = {
@ -63,7 +68,7 @@ in {
files = mkOption {
default = {};
type = attrsOf (fileTypeRelativeTo cfg.directory);
type = attrsOf (fileTypeRelativeTo' cfg.directory);
example = {".config/foo.txt".source = "Hello World";};
description = "Files to be managed by Hjem";
};
@ -84,7 +89,7 @@ in {
};
files = mkOption {
default = {};
type = attrsOf (fileTypeRelativeTo cfg.xdg.cache.directory);
type = attrsOf (fileTypeRelativeTo' cfg.xdg.cache.directory);
example = {"foo.txt".source = "Hello World";};
description = "Cache files to be managed by Hjem";
};
@ -105,7 +110,7 @@ in {
};
files = mkOption {
default = {};
type = attrsOf (fileTypeRelativeTo cfg.xdg.config.directory);
type = attrsOf (fileTypeRelativeTo' cfg.xdg.config.directory);
example = {"foo.txt".source = "Hello World";};
description = "Config files to be managed by Hjem";
};
@ -126,7 +131,7 @@ in {
};
files = mkOption {
default = {};
type = attrsOf (fileTypeRelativeTo cfg.xdg.data.directory);
type = attrsOf (fileTypeRelativeTo' cfg.xdg.data.directory);
example = {"foo.txt".source = "Hello World";};
description = "data files to be managed by Hjem";
};
@ -147,7 +152,7 @@ in {
};
files = mkOption {
default = {};
type = attrsOf (fileTypeRelativeTo cfg.xdg.state.directory);
type = attrsOf (fileTypeRelativeTo' cfg.xdg.state.directory);
example = {"foo.txt".source = "Hello World";};
description = "state files to be managed by Hjem";
};

View file

@ -1,5 +1,6 @@
{
config,
hjem-lib,
lib,
options,
pkgs,
@ -10,7 +11,7 @@
inherit (lib.options) literalExpression mkOption;
inherit (lib.strings) optionalString;
inherit (lib.trivial) pipe;
inherit (lib.types) attrs attrsOf bool listOf nullOr package raw submoduleWith either singleLineStr;
inherit (lib.types) attrs attrsOf bool either listOf nullOr package raw singleLineStr submoduleWith;
inherit (lib.meta) getExe;
inherit (builtins) filter attrNames attrValues mapAttrs getAttr concatLists concatStringsSep typeOf toJSON concatMap;
@ -78,7 +79,7 @@
specialArgs =
cfg.specialArgs
// {
inherit pkgs;
inherit hjem-lib pkgs;
osConfig = config;
osOptions = options;
};