Merge pull request #271 from Pacman99/lib-rearrange
Rearrange and cleanup lib
This commit is contained in:
commit
30412b16b9
|
@ -79,7 +79,7 @@
|
|||
"utils": "utils_2"
|
||||
},
|
||||
"locked": {
|
||||
"narHash": "sha256-oTiKYoR210VwjomzfSn/pCJ3immdUDRUPbYTDGaPFn8=",
|
||||
"narHash": "sha256-9JsKDtgLSmAkcaKRD4Ycttip1jpO9dVVaRwclWH0V8E=",
|
||||
"path": "./lib",
|
||||
"type": "path"
|
||||
},
|
||||
|
|
|
@ -12,43 +12,8 @@ rec {
|
|||
# Generate an attribute set by mapping a function over a list of values.
|
||||
genAttrs' = values: f: lib.listToAttrs (map f values);
|
||||
|
||||
# Convert a list of file paths to attribute set where
|
||||
# the key is the folder or filename stripped of nix
|
||||
# extension and imported content of the file as value.
|
||||
#
|
||||
pathsToImportedAttrs = paths:
|
||||
let
|
||||
paths' = lib.filter
|
||||
(path: lib.hasSuffix ".nix" path
|
||||
|| lib.pathExists "${path}/default.nix")
|
||||
paths;
|
||||
in
|
||||
genAttrs' paths' (path: {
|
||||
name = lib.removeSuffix ".nix"
|
||||
# Safe as long this is just used as a name
|
||||
(builtins.unsafeDiscardStringContext (baseNameOf path));
|
||||
value = import path;
|
||||
});
|
||||
|
||||
importHosts = dir:
|
||||
lib.os.recImport {
|
||||
inherit dir;
|
||||
_import = base: {
|
||||
modules = import "${toString dir}/${base}.nix";
|
||||
};
|
||||
};
|
||||
|
||||
concatAttrs = lib.fold (attr: sum: lib.recursiveUpdate sum attr) { };
|
||||
|
||||
# Filter out packages that support given system and follow flake check requirements
|
||||
filterPackages = system: packages:
|
||||
let
|
||||
# Everything that nix flake check requires for the packages output
|
||||
filter = (n: v: with v; let platforms = meta.hydraPlatforms or meta.platforms or [ ]; in
|
||||
lib.isDerivation v && !meta.broken && builtins.elem system platforms);
|
||||
in
|
||||
lib.filterAttrs filter packages;
|
||||
|
||||
safeReadDir = path:
|
||||
lib.optionalAttrs (builtins.pathExists (toString path)) (builtins.readDir (toString path));
|
||||
}
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
{ lib, utils }:
|
||||
{
|
||||
# pkgImport :: Nixpkgs -> Overlays -> System -> Pkgs
|
||||
pkgImport = nixpkgs: overlays: system:
|
||||
import nixpkgs {
|
||||
inherit system overlays;
|
||||
config = { allowUnfree = true; };
|
||||
};
|
||||
|
||||
profileMap = list: map (profile: profile.default) (lib.flatten list);
|
||||
|
||||
mkNodes = import ./mkNodes.nix { inherit lib; };
|
||||
|
||||
mkHosts = import ./mkHosts.nix { inherit lib; };
|
||||
|
||||
mkSuites = import ./mkSuites.nix { inherit lib; };
|
||||
|
||||
mkProfileAttrs = import ./mkProfileAttrs.nix { inherit lib; };
|
||||
|
||||
mkPkgs = import ./mkPkgs.nix { inherit lib utils; };
|
||||
|
||||
recImport = import ./recImport.nix { inherit lib; };
|
||||
|
||||
devosSystem = import ./devosSystem.nix { inherit lib; };
|
||||
|
||||
mkHomeConfigurations = import ./mkHomeConfigurations.nix { inherit lib; };
|
||||
|
||||
mkPackages = import ./mkPackages.nix { inherit lib; };
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
{ lib }:
|
||||
|
||||
# dependencies to return a builder
|
||||
{ self, inputs }:
|
||||
|
||||
{ modules, specialArgs, ... } @ args:
|
||||
let inherit (specialArgs.channel.input.lib) nixosSystem; in
|
||||
nixosSystem
|
||||
(args // {
|
||||
modules =
|
||||
let
|
||||
fullHostConfig = (nixosSystem (args // { inherit modules; })).config;
|
||||
|
||||
isoConfig = (nixosSystem
|
||||
(args // {
|
||||
modules = modules ++ [
|
||||
(lib.modules.iso { inherit self inputs fullHostConfig; })
|
||||
];
|
||||
})).config;
|
||||
|
||||
hmConfig = (nixosSystem
|
||||
(args // {
|
||||
modules = modules ++ [
|
||||
(lib.modules.hmConfig)
|
||||
];
|
||||
})).config;
|
||||
in
|
||||
modules ++ [{
|
||||
system.build = {
|
||||
iso = isoConfig.system.build.isoImage;
|
||||
homes = hmConfig.home-manager.users;
|
||||
};
|
||||
lib = {
|
||||
inherit specialArgs;
|
||||
testModule = {
|
||||
imports = modules;
|
||||
};
|
||||
};
|
||||
}];
|
||||
})
|
|
@ -1,57 +0,0 @@
|
|||
{ lib }:
|
||||
|
||||
{ self, nixos, inputs, dir, extern, suites, overrides, multiPkgs }:
|
||||
let
|
||||
defaultSystem = "x86_64-linux";
|
||||
|
||||
modules = with lib.modules; {
|
||||
modOverrides = modOverrides { inherit overrides; };
|
||||
hmDefaults = hmDefaults {
|
||||
inherit extern;
|
||||
inherit (self) homeModules;
|
||||
userSuites = suites.user;
|
||||
};
|
||||
globalDefaults = globalDefaults {
|
||||
inherit self nixos inputs multiPkgs;
|
||||
};
|
||||
cachix = cachix { inherit self; };
|
||||
flakeModules = flakeModules { inherit self extern; };
|
||||
};
|
||||
|
||||
specialArgs = extern.specialArgs // { suites = suites.system; };
|
||||
|
||||
mkHostConfig = hostName:
|
||||
let
|
||||
local = {
|
||||
require = [
|
||||
"${dir}/${hostName}.nix"
|
||||
];
|
||||
|
||||
networking = { inherit hostName; };
|
||||
|
||||
_module.args = {
|
||||
self = self;
|
||||
hosts = builtins.mapAttrs (_: host: host.config)
|
||||
(removeAttrs hosts [ hostName ]);
|
||||
};
|
||||
|
||||
lib = { inherit specialArgs; };
|
||||
lib.testModule = {
|
||||
imports = builtins.attrValues modules;
|
||||
};
|
||||
|
||||
};
|
||||
in
|
||||
lib.os.devosSystem {
|
||||
inherit self nixos inputs specialArgs;
|
||||
system = defaultSystem;
|
||||
modules = modules // { inherit local; };
|
||||
};
|
||||
|
||||
hosts = lib.os.recImport
|
||||
{
|
||||
inherit dir;
|
||||
_import = mkHostConfig;
|
||||
};
|
||||
in
|
||||
hosts
|
|
@ -1,16 +0,0 @@
|
|||
{ lib }:
|
||||
|
||||
/**
|
||||
Synopsis: mkNodes _nixosConfigurations_
|
||||
|
||||
Generate the `nodes` attribute expected by deploy-rs
|
||||
where _nixosConfigurations_ are `nodes`.
|
||||
**/
|
||||
deploy: lib.mapAttrs (_: config: {
|
||||
hostname = config.config.networking.hostName;
|
||||
|
||||
profiles.system = {
|
||||
user = "root";
|
||||
path = deploy.lib.x86_64-linux.activate.nixos config;
|
||||
};
|
||||
})
|
|
@ -1,17 +0,0 @@
|
|||
{ lib }:
|
||||
|
||||
{ overlay, overlays, pkgs }:
|
||||
let
|
||||
packagesNames = lib.attrNames (overlay null null)
|
||||
++ lib.attrNames (lib.concatAttrs
|
||||
(lib.attrValues
|
||||
(lib.mapAttrs (_: v: v null null) overlays)
|
||||
)
|
||||
);
|
||||
in
|
||||
lib.fold
|
||||
(key: sum: lib.recursiveUpdate sum {
|
||||
${key} = pkgs.${key};
|
||||
})
|
||||
{ }
|
||||
packagesNames
|
|
@ -1,25 +0,0 @@
|
|||
{ lib, utils }:
|
||||
|
||||
{ self, nixos, inputs, extern, overrides }:
|
||||
(utils.lib.eachDefaultSystem
|
||||
(system:
|
||||
let
|
||||
overridePkgs = lib.os.pkgImport inputs.override [ ] system;
|
||||
overridesOverlay = overrides.packages;
|
||||
|
||||
overlays = [
|
||||
(final: prev: {
|
||||
lib = prev.lib.extend (lfinal: lprev: {
|
||||
inherit lib;
|
||||
inherit (lib) nixosSystem;
|
||||
});
|
||||
})
|
||||
(overridesOverlay overridePkgs)
|
||||
self.overlay
|
||||
]
|
||||
++ extern.overlays
|
||||
++ (lib.attrValues self.overlays);
|
||||
in
|
||||
{ pkgs = lib.os.pkgImport nixos overlays system; }
|
||||
)
|
||||
).pkgs
|
|
@ -1,35 +0,0 @@
|
|||
{ lib }:
|
||||
|
||||
let mkProfileAttrs =
|
||||
/**
|
||||
Synopsis: mkProfileAttrs _path_
|
||||
|
||||
Recursively collect the subdirs of _path_ containing a default.nix into attrs.
|
||||
This sets a contract, eliminating ambiguity for _default.nix_ living under the
|
||||
profile directory.
|
||||
|
||||
Example:
|
||||
let profiles = mkProfileAttrs ./profiles; in
|
||||
assert profiles ? core.default; 0
|
||||
**/
|
||||
dir:
|
||||
let
|
||||
imports =
|
||||
let
|
||||
files = lib.safeReadDir dir;
|
||||
|
||||
p = n: v:
|
||||
v == "directory"
|
||||
&& n != "profiles";
|
||||
in
|
||||
lib.filterAttrs p files;
|
||||
|
||||
f = n: _:
|
||||
lib.optionalAttrs
|
||||
(lib.pathExists "${dir}/${n}/default.nix")
|
||||
{ default = "${dir}/${n}"; }
|
||||
// mkProfileAttrs "${dir}/${n}";
|
||||
in
|
||||
lib.mapAttrs f imports;
|
||||
in mkProfileAttrs
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
{ lib }:
|
||||
|
||||
{ suites, profiles } @ args:
|
||||
let
|
||||
inherit (lib) os;
|
||||
|
||||
profileSet = lib.genAttrs' profiles (path: {
|
||||
name = baseNameOf path;
|
||||
value = os.mkProfileAttrs (toString path);
|
||||
});
|
||||
|
||||
definedSuites = suites profileSet;
|
||||
|
||||
allProfiles = lib.collectProfiles profileSet;
|
||||
in
|
||||
lib.mapAttrs (_: v: os.profileMap v) definedSuites // {
|
||||
inherit allProfiles;
|
||||
}
|
||||
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
{ lib }:
|
||||
|
||||
{ dir, _import ? base: import "${dir}/${base}.nix" }:
|
||||
lib.mapFilterAttrs
|
||||
(_: v: v != null)
|
||||
(n: v:
|
||||
if n != "default.nix" && lib.hasSuffix ".nix" n && v == "regular"
|
||||
then
|
||||
let name = lib.removeSuffix ".nix" n; in lib.nameValuePair (name) (_import name)
|
||||
else
|
||||
lib.nameValuePair ("") (null))
|
||||
(lib.safeReadDir dir)
|
|
@ -10,31 +10,23 @@
|
|||
|
||||
outputs = inputs@{ self, nixpkgs, deploy, devshell, utils, ... }:
|
||||
let
|
||||
|
||||
lib = nixpkgs.lib.makeExtensible (self:
|
||||
let
|
||||
attrs = import ./attrs.nix { lib = nixpkgs.lib // self; };
|
||||
lists = import ./lists.nix { lib = nixpkgs.lib // self; };
|
||||
strings = import ./strings.nix { lib = nixpkgs.lib // self; };
|
||||
modules = import ./modules.nix { lib = nixpkgs.lib // self; };
|
||||
in
|
||||
|
||||
utils.lib
|
||||
|
||||
//
|
||||
|
||||
{
|
||||
os = import ./devos {
|
||||
lib = nixpkgs.lib // self;
|
||||
inherit utils;
|
||||
};
|
||||
let combinedLib = nixpkgs.lib // self; in
|
||||
with self;
|
||||
utils.lib // {
|
||||
attrs = import ./attrs.nix { lib = combinedLib; };
|
||||
lists = import ./lists.nix { lib = combinedLib; };
|
||||
strings = import ./strings.nix { lib = combinedLib; };
|
||||
modules = import ./modules.nix { lib = combinedLib; };
|
||||
importers = import ./importers.nix { lib = combinedLib; };
|
||||
generators = import ./generators.nix { lib = combinedLib; };
|
||||
|
||||
mkFlake = {
|
||||
__functor = import ./mkFlake {
|
||||
lib = nixpkgs.lib // self;
|
||||
lib = combinedLib;
|
||||
inherit deploy;
|
||||
};
|
||||
evalArgs = import ./mkFlake/evalArgs.nix { lib = nixpkgs.lib // self; };
|
||||
evalArgs = import ./mkFlake/evalArgs.nix { lib = combinedLib; };
|
||||
};
|
||||
|
||||
pkgs-lib = import ./pkgs-lib {
|
||||
|
@ -42,28 +34,26 @@
|
|||
inherit deploy devshell;
|
||||
};
|
||||
|
||||
inherit (attrs)
|
||||
mapFilterAttrs
|
||||
genAttrs'
|
||||
safeReadDir
|
||||
pathsToImportedAttrs
|
||||
concatAttrs
|
||||
filterPackages
|
||||
importHosts;
|
||||
inherit (lists) pathsIn collectProfiles unifyOverlays;
|
||||
inherit (attrs) mapFilterAttrs genAttrs' safeReadDir concatAttrs;
|
||||
inherit (lists) profileMap collectProfiles unifyOverlays;
|
||||
inherit (strings) rgxToString;
|
||||
inherit modules;
|
||||
inherit (importers) mkProfileAttrs pathsIn importHosts;
|
||||
inherit (generators) mkSuites mkDeployNodes mkHomeConfigurations;
|
||||
}
|
||||
);
|
||||
|
||||
in
|
||||
|
||||
{
|
||||
lib = utils.lib // {
|
||||
lib = with lib; utils.lib // {
|
||||
inherit attrs lists modules importers generators;
|
||||
inherit (lib)
|
||||
mkFlake pathsIn importHosts;
|
||||
mkFlake
|
||||
pathsIn
|
||||
importHosts
|
||||
mkDeployNodes
|
||||
mkHomeConfigurations;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
|
46
lib/generators.nix
Normal file
46
lib/generators.nix
Normal file
|
@ -0,0 +1,46 @@
|
|||
{ lib }:
|
||||
{
|
||||
mkHomeConfigurations = nixosConfigurations:
|
||||
with lib;
|
||||
let
|
||||
mkHomes = host: config:
|
||||
mapAttrs' (user: v: nameValuePair "${user}@${host}" v.home)
|
||||
config.config.system.build.homes;
|
||||
|
||||
hmConfigs = mapAttrs mkHomes nixosConfigurations;
|
||||
|
||||
in
|
||||
foldl recursiveUpdate { } (attrValues hmConfigs);
|
||||
|
||||
mkDeployNodes =
|
||||
/**
|
||||
Synopsis: mkNodes _nixosConfigurations_
|
||||
|
||||
Generate the `nodes` attribute expected by deploy-rs
|
||||
where _nixosConfigurations_ are `nodes`.
|
||||
**/
|
||||
|
||||
deploy: lib.mapAttrs (_: config: {
|
||||
hostname = config.config.networking.hostName;
|
||||
|
||||
profiles.system = {
|
||||
user = "root";
|
||||
path = deploy.lib.x86_64-linux.activate.nixos config;
|
||||
};
|
||||
});
|
||||
|
||||
mkSuites = { suites, profiles }:
|
||||
let
|
||||
profileSet = lib.genAttrs' profiles (path: {
|
||||
name = baseNameOf path;
|
||||
value = lib.mkProfileAttrs (toString path);
|
||||
});
|
||||
|
||||
definedSuites = suites profileSet;
|
||||
|
||||
allProfiles = lib.collectProfiles profileSet;
|
||||
in
|
||||
lib.mapAttrs (_: v: lib.profileMap v) definedSuites // {
|
||||
inherit allProfiles;
|
||||
};
|
||||
}
|
64
lib/importers.nix
Normal file
64
lib/importers.nix
Normal file
|
@ -0,0 +1,64 @@
|
|||
{ lib }:
|
||||
let
|
||||
recImport = { dir, _import ? base: import "${dir}/${base}.nix" }:
|
||||
lib.mapFilterAttrs
|
||||
(_: v: v != null)
|
||||
(n: v:
|
||||
if n != "default.nix" && lib.hasSuffix ".nix" n && v == "regular"
|
||||
then
|
||||
let name = lib.removeSuffix ".nix" n; in lib.nameValuePair (name) (_import name)
|
||||
else
|
||||
lib.nameValuePair ("") (null))
|
||||
(lib.safeReadDir dir);
|
||||
|
||||
mkProfileAttrs =
|
||||
/**
|
||||
Synopsis: mkProfileAttrs _path_
|
||||
|
||||
Recursively collect the subdirs of _path_ containing a default.nix into attrs.
|
||||
This sets a contract, eliminating ambiguity for _default.nix_ living under the
|
||||
profile directory.
|
||||
|
||||
Example:
|
||||
let profiles = mkProfileAttrs ./profiles; in
|
||||
assert profiles ? core.default; 0
|
||||
**/
|
||||
dir:
|
||||
let
|
||||
imports =
|
||||
let
|
||||
files = lib.safeReadDir dir;
|
||||
|
||||
p = n: v:
|
||||
v == "directory"
|
||||
&& n != "profiles";
|
||||
in
|
||||
lib.filterAttrs p files;
|
||||
|
||||
f = n: _:
|
||||
lib.optionalAttrs
|
||||
(lib.pathExists "${dir}/${n}/default.nix")
|
||||
{ default = "${dir}/${n}"; }
|
||||
// mkProfileAttrs "${dir}/${n}";
|
||||
in
|
||||
lib.mapAttrs f imports;
|
||||
|
||||
in
|
||||
{
|
||||
inherit recImport mkProfileAttrs;
|
||||
|
||||
pathsIn = dir:
|
||||
let
|
||||
fullPath = name: "${toString dir}/${name}";
|
||||
in
|
||||
map fullPath (lib.attrNames (lib.safeReadDir dir));
|
||||
|
||||
importHosts = dir:
|
||||
recImport {
|
||||
inherit dir;
|
||||
_import = base: {
|
||||
modules = import "${toString dir}/${base}.nix";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -3,19 +3,17 @@
|
|||
collectProfiles = set:
|
||||
let
|
||||
collectNestedProfiles = set:
|
||||
lib.mapAttrsToList (n: v:
|
||||
if builtins.isAttrs v then
|
||||
[ v.default or null ] ++ collectNestedProfiles v
|
||||
else null
|
||||
) set;
|
||||
lib.mapAttrsToList
|
||||
(n: v:
|
||||
if builtins.isAttrs v then
|
||||
[ v.default or null ] ++ collectNestedProfiles v
|
||||
else null
|
||||
)
|
||||
set;
|
||||
in
|
||||
builtins.filter (x: x != null) (lib.flatten (collectNestedProfiles set));
|
||||
builtins.filter (x: x != null) (lib.flatten (collectNestedProfiles set));
|
||||
|
||||
pathsIn = dir:
|
||||
let
|
||||
fullPath = name: "${toString dir}/${name}";
|
||||
in
|
||||
map fullPath (lib.attrNames (lib.safeReadDir dir));
|
||||
profileMap = list: map (profile: profile.default) (lib.flatten list);
|
||||
|
||||
unifyOverlays = channels: map (o: if builtins.isFunction (o null null) then o channels else o);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
{ lib, deploy }:
|
||||
let
|
||||
inherit (lib) os;
|
||||
inherit (builtins) mapAttrs attrNames attrValues head isFunction;
|
||||
in
|
||||
|
||||
|
@ -66,16 +65,16 @@ lib.systemFlake (lib.mergeAny
|
|||
modules = cfg.nixos.hostDefaults.externalModules ++ defaultModules;
|
||||
builder = args: args.specialArgs.channel.input.lib.nixosSystem (lib.mergeAny args {
|
||||
# So modules and functions can create their own version of the build
|
||||
modules = [ { lib.builderArgs = args; } ];
|
||||
modules = [{ lib.builderArgs = args; }];
|
||||
});
|
||||
};
|
||||
|
||||
nixosModules = lib.exporter.modulesFromList cfg.nixos.hostDefaults.modules;
|
||||
|
||||
homeModules = lib.exporter.modulesFromList cfg.home.modules;
|
||||
homeConfigurations = os.mkHomeConfigurations self.nixosConfigurations;
|
||||
homeConfigurations = lib.mkHomeConfigurations self.nixosConfigurations;
|
||||
|
||||
deploy.nodes = os.mkNodes deploy self.nixosConfigurations;
|
||||
deploy.nodes = lib.mkDeployNodes deploy self.nixosConfigurations;
|
||||
|
||||
overlays = lib.exporter.overlaysFromChannelsExporter {
|
||||
# since we can't detect overlays owned by self
|
||||
|
|
|
@ -4,8 +4,6 @@
|
|||
let
|
||||
argOpts = with lib; { config, ... }:
|
||||
let
|
||||
inherit (lib) os;
|
||||
|
||||
cfg = config;
|
||||
inherit (config) self;
|
||||
|
||||
|
@ -188,7 +186,7 @@ let
|
|||
suites = mkOption {
|
||||
type = pathTo (functionTo attrs);
|
||||
default = _: { };
|
||||
apply = suites: os.mkSuites {
|
||||
apply = suites: lib.mkSuites {
|
||||
inherit suites;
|
||||
inherit (config) profiles;
|
||||
};
|
||||
|
|
|
@ -37,25 +37,6 @@ lib.runTests {
|
|||
];
|
||||
};
|
||||
|
||||
testPathsToImportedAttrs = {
|
||||
expr =
|
||||
pathsToImportedAttrs [
|
||||
(toString ./testPathsToImportedAttrs/dir)
|
||||
./testPathsToImportedAttrs/foo.nix
|
||||
./testPathsToImportedAttrs/bar.nix
|
||||
./testPathsToImportedAttrs/t.nix
|
||||
./testPathsToImportedAttrs/f.nix
|
||||
];
|
||||
|
||||
expected = {
|
||||
dir = { a = 5; };
|
||||
foo = { bar = 1; };
|
||||
bar = { foo = 2; };
|
||||
t = true;
|
||||
f = false;
|
||||
};
|
||||
};
|
||||
|
||||
testRgxToString = lib.testAllTrue [
|
||||
(rgxToString ".+x" "vxk" == "vx")
|
||||
(rgxToString "^fo" "foo" == "fo")
|
||||
|
@ -72,7 +53,7 @@ lib.runTests {
|
|||
};
|
||||
|
||||
testSuites = {
|
||||
expr = os.mkSuites {
|
||||
expr = mkSuites {
|
||||
suites = { profiles, ... }: with profiles; {
|
||||
bar = [ foo ];
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue