83 lines
2.8 KiB
Nix
83 lines
2.8 KiB
Nix
{
|
|
description = "A flake that provides a set of useful helper functions";
|
|
|
|
inputs = {
|
|
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
|
};
|
|
|
|
outputs = { self, nixpkgs }: let
|
|
inherit (nixpkgs) lib;
|
|
in rec {
|
|
mkIfElse = with lib; p: yes: no: mkMerge [
|
|
(mkIf p yes)
|
|
(mkIf (!p) no)
|
|
];
|
|
|
|
mkSymlinkActivationScript = {target, source}: ''
|
|
if [ -e ${source} ]; then
|
|
if [ -L ${target} ] || [ -e ${target} ]; then
|
|
rm -rf ${target}
|
|
fi
|
|
ln -s ${source} ${target}
|
|
fi
|
|
'';
|
|
|
|
attrByStrPath = strPath: set: lib.attrByPath (lib.strings.splitString "." strPath) null set;
|
|
|
|
setAttrByStrPath = strPath: value: lib.setAttrByPath (lib.strings.splitString "." strPath) value;
|
|
|
|
homeFilesFromStringAttrs = { dir, attrs, exec ? false }: mergeListsToAttrs (map (x: "${dir}/${x}") (builtins.attrNames attrs)) (map (y: {
|
|
text = y;
|
|
executable = exec;
|
|
}) (builtins.attrValues attrs));
|
|
|
|
prependShebangToString = x: "#!/usr/bin/env bash\n" + x;
|
|
|
|
localBinFromStringAttrs = attrs: homeFilesFromStringAttrs {
|
|
dir = ".local/bin";
|
|
attrs = builtins.mapAttrs (name: value: prependShebangToString value) attrs;
|
|
exec = true;
|
|
};
|
|
|
|
mergeListsToAttrs = names: values: lib.listToAttrs (lib.zipListsWith (name: value: { inherit name value; }) names values);
|
|
|
|
mkEnumFromDirContents = dir: lib.types.enum (map (name: lib.removeSuffix ".nix" name) (builtins.attrNames (builtins.readDir dir)));
|
|
|
|
mkOptionalListOfType = type: lib.types.either (type) (lib.types.listOf (type));
|
|
|
|
mkMirroredModulesFlakeModule = moduleName: func: {
|
|
flake.nixosModules.${moduleName} = func;
|
|
|
|
flake.homeModules.${moduleName} = func;
|
|
};
|
|
|
|
mkStandardFeature = {
|
|
flakeModule = moduleName: optionName: optionString: (mkMirroredModulesFlakeModule "${moduleName}Options" ({ lib, ... }: {
|
|
options = setAttrByStrPath optionName (lib.mkEnableOption optionString);
|
|
}));
|
|
|
|
nixosModule = { config, optionName }: systemConfig: {
|
|
config = lib.mkIf (attrByStrPath optionName config) (mkIfElse (config.features.nixos.home-manager.enable) {
|
|
home-manager.users.${config.info.userName} = setAttrByStrPath optionName (lib.mkDefault true);
|
|
} systemConfig);
|
|
};
|
|
|
|
homeModule = { config, optionName }: homeConfig: {
|
|
config = lib.mkIf (attrByStrPath optionName config) homeConfig;
|
|
};
|
|
};
|
|
|
|
mkRedundantOptionDescription = name: value: "The functionality of this option completely depends on `${name}` being `${value}`, and will not be evaluated otherwise.";
|
|
|
|
mkRedundantOption = name: value: attrs: lib.mkOption {
|
|
inherit (attrs) type default;
|
|
description = ''
|
|
${attrs.description}
|
|
|
|
|
|
${mkRedundantOptionDescription name value}
|
|
'';
|
|
};
|
|
};
|
|
}
|