nixos/local/lib/default.nix

37 lines
1.6 KiB
Nix
Raw Normal View History

2023-07-21 21:57:06 +08:00
lib:
2023-07-08 16:26:12 +08:00
{
2023-07-22 00:09:29 +08:00
attrsToList = Attrs: builtins.map ( name: { inherit name; value = Attrs.${name}; } ) ( builtins.attrNames Attrs );
mkConditional = condition: trueResult: falseResult: let inherit (lib) mkMerge mkIf; in
mkMerge [ ( mkIf condition trueResult ) ( mkIf (!condition) falseResult ) ];
# Behaviors of these two NixOS modules would be different:
# { pkgs, ... }@inputs: { environment.systemPackages = [ pkgs.hello ]; }
# inputs: { environment.systemPackages = [ pkgs.hello ]; }
# The second one would failed to evaluate because nixpkgs would not pass pkgs to it.
# So that we wrote a wrapper to make it always works like the first one.
mkModules = moduleList: { pkgs, ... }@inputs:
{
imports = builtins.map
(
2023-07-27 22:24:01 +08:00
let handle = module:
if ( builtins.typeOf module ) == "path" then handle import module
else if ( builtins.typeOf module ) == "lambda" then module inputs
else module;
in handle
2023-07-22 00:09:29 +08:00
) moduleList;
};
# from: https://github.com/NixOS/nix/issues/3759
stripeTabs = text:
let
# Whether all lines start with a tab (or is empty)
2023-07-22 00:14:07 +08:00
shouldStripTab = lines: builtins.all (line: (line == "") || (lib.strings.hasPrefix " " line)) lines;
2023-07-22 00:09:29 +08:00
# Strip a leading tab from all lines
2023-07-22 00:14:07 +08:00
stripTab = lines: builtins.map (line: lib.strings.removePrefix " " line) lines;
2023-07-22 00:09:29 +08:00
# Strip tabs recursively until there are none
stripTabs = lines: if (shouldStripTab lines) then (stripTabs (stripTab lines)) else lines;
in
# Split into lines. Strip leading tabs. Concat back to string.
2023-07-22 00:14:07 +08:00
builtins.concatStringsSep "\n" (stripTabs (lib.strings.splitString "\n" text));
2023-07-08 16:26:12 +08:00
}