refactor(flake)!: export packages using a flake module

Also stop passing `outputs` around, prefer `inputs.self`
Also put all `auto_` in `readOnly` `internal` options
- `autoModules`, `autoConfigurations` and `autoPackages`
Do not export packages as overlay (causes an infinite recursion)
- this is documented on the two places it has effect on
-- in `./overlays/default.nix` and `./modules/flake/configurations.nix`
-- in `autoConfigurations` we manually extend `pkgs` with the flake packages
Allow packages to say what `systems` they are compatible with
- See `./pkgs/swww/systems.nix` - disabled for all systems
- See `./pkgs/pngpaste/systems.nix` - enabled only for `darwin` targets
This commit is contained in:
reo101 2024-07-29 01:35:14 +03:00
parent e45db02cd5
commit 2941536b80
Signed by: reo101
GPG key ID: 675AA7EF13964ACB
36 changed files with 241 additions and 118 deletions

View file

@ -1,19 +1,23 @@
{ lib, config, self, inputs, withSystem, ... }:
let
outputs = self;
inherit (import ../../nix/utils.nix { inherit lib self; })
inherit (import ../../nix/utils.nix { inherit lib config self; })
and
hasFiles
hasDirectories
recurseDir
configuration-type-to-outputs-modules
configuration-type-to-outputs-hosts;
configuration-type-to-outputs-modules;
in
let
# Configuration helpers
configurationTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager"];
# `pkgs` with flake's overlays
# NOTE: done here to avoid infinite recursion
pkgs' = system:
(withSystem system ({ pkgs, ... }: pkgs)).extend
(final: prev: inputs.self.packages.${system});
homeManagerModule = { root, system, hostname, users ? null }: {
home-manager = {
# Use same `pkgs` instance as system (i.e. carry over overlays)
@ -24,7 +28,7 @@ let
sharedModules = builtins.attrValues config.flake.${configuration-type-to-outputs-modules "home-manager"};
# Pass in `inputs`, `outputs` and maybe `meta`
extraSpecialArgs = {
inherit inputs outputs;
inherit inputs;
# TODO: meta?
inherit hostname;
};
@ -42,7 +46,7 @@ let
mkNixosHost = args @ { root, system, hostname, users }: inputs.nixpkgs.lib.nixosSystem {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
pkgs = pkgs' system;
modules = [
# Main configuration
@ -62,14 +66,14 @@ let
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "nixos"});
specialArgs = {
inherit inputs outputs;
inherit inputs;
};
};
mkNixOnDroidHost = args @ { root, system, hostname }: inputs.nix-on-droid.lib.nixOnDroidConfiguration {
# NOTE: inferred by `pkgs.system`
# inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
pkgs = pkgs' system;
modules = [
# Main configuration
@ -79,7 +83,7 @@ let
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "nix-on-droid"});
extraSpecialArgs = {
inherit inputs outputs;
inherit inputs;
};
home-manager-path = inputs.home-manager.outPath;
@ -87,7 +91,7 @@ let
mkNixDarwinHost = args @ { root, system, hostname, users }: inputs.nix-darwin.lib.darwinSystem {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
pkgs = pkgs' system;
modules = [
# Main configuration
@ -102,20 +106,20 @@ let
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "nix-darwin"});
specialArgs = {
inherit inputs outputs;
inherit inputs;
};
};
mkHomeManagerHost = args @ { root, system, hostname }: inputs.home-manager.lib.homeManagerConfiguration {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
pkgs = pkgs' system;
modules = [
"${root}/home.nix"
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "home-manager"});
extraSpecialArgs = {
inherit inputs outputs;
inherit inputs;
inherit hostname;
};
};
@ -182,6 +186,20 @@ in
type = types.path;
default = "${submodule.config.baseDir}/${configurationType}";
};
# TODO: split hosts and configurations?
resultHosts = lib.mkOption {
description = ''
The resulting automatic packages
'';
# TODO: specify
type = types.unspecified;
readOnly = true;
internal = true;
default =
lib.optionalAttrs
config.flake.autoConfigurations.${configurationType}.enable
(recurseDir config.flake.autoConfigurations.${configurationType}.dir);
};
};
};
default = {};
@ -194,21 +212,7 @@ in
};
config = {
flake = let
autoHosts =
lib.pipe
configurationTypes
[
(builtins.map
(configurationType:
lib.nameValuePair
"${configuration-type-to-outputs-hosts configurationType}"
(if config.flake.autoConfigurations.${configurationType}.enable
then recurseDir config.flake.autoConfigurations.${configurationType}.dir
else { })))
builtins.listToAttrs
];
in autoHosts // {
flake = {
# Configurations
nixosConfigurations =
createConfigurations
@ -231,7 +235,7 @@ in
(lib.strings.removeSuffix ".nix")
(builtins.attrNames (configurationFiles."home" or { })));
})
self.${configuration-type-to-outputs-hosts "nixos"};
config.flake.autoConfigurations.nixos.resultHosts;
nixOnDroidConfigurations =
createConfigurations
@ -248,7 +252,7 @@ in
inherit system;
hostname = host;
})
self.${configuration-type-to-outputs-hosts "nix-on-droid"};
config.flake.autoConfigurations.nix-on-droid.resultHosts;
darwinConfigurations =
createConfigurations
@ -271,7 +275,7 @@ in
(lib.strings.removeSuffix ".nix")
(builtins.attrNames (configurationFiles."home" or { })));
})
self.${configuration-type-to-outputs-hosts "nix-darwin"};
config.flake.autoConfigurations.nix-darwin.resultHosts;
homeConfigurations =
createConfigurations
@ -288,7 +292,7 @@ in
inherit system;
hostname = host;
})
self.${configuration-type-to-outputs-hosts "home-manager"};
config.flake.autoConfigurations.home-manager.resultHosts;
};
};
}

View file

@ -1,7 +1,7 @@
{ lib, config, self, inputs, ... }:
let
inherit (import ../../nix/utils.nix { inherit lib self; })
inherit (import ../../nix/utils.nix { inherit lib config self; })
accumulateHosts
configuration-type-to-deploy-type;
in

View file

@ -1,8 +1,7 @@
{ lib, config, self, inputs, ... }:
let
outputs = self;
inherit (import ../../nix/utils.nix { inherit lib self; })
inherit (import ../../nix/utils.nix { inherit lib config self; })
eq
and
hasFiles
@ -12,7 +11,7 @@ let
# Modules helpers
moduleTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager" "flake"];
createModules = baseDir: { passthru ? { inherit inputs outputs; }, ... }:
createModules = baseDir: { passthru ? { inherit inputs; }, ... }:
lib.pipe baseDir [
# Read given directory
builtins.readDir
@ -55,6 +54,7 @@ let
if and [
(builtins.isFunction
module)
# FIXME: check for subset, not `eq`
(eq
(lib.pipe module [ builtins.functionArgs builtins.attrNames ])
(lib.pipe passthru [ builtins.attrNames ]))
@ -104,6 +104,19 @@ in
type = types.path;
default = "${submodule.config.baseDir}/${moduleType}";
};
result = lib.mkOption {
description = ''
The resulting automatic packages
'';
# TODO: specify
type = types.unspecified;
readOnly = true;
internal = true;
default =
lib.optionalAttrs
config.flake.autoModules.${moduleType}.enable
(createModules config.flake.autoModules.${moduleType}.dir { });
};
};
};
default = {};
@ -123,22 +136,13 @@ in
[
(builtins.map
(moduleType:
lib.nameValuePair
"${configuration-type-to-outputs-modules moduleType}"
(if config.flake.autoModules.${moduleType}.enable
then createModules config.flake.autoModules.${moduleType}.dir { }
else { })))
let
name = "${configuration-type-to-outputs-modules moduleType}";
value = config.flake.autoModules.${moduleType}.result;
in
lib.nameValuePair name value))
builtins.listToAttrs
];
in {
# NOTE: manually inheriting generated modules to avoid recursion
# (`autoModules` depends on `config.flake` itself)
inherit (autoModules)
nixosModules
nixOnDroidModules
darwinModules
homeManagerModules
flakeModules;
};
in autoModules;
};
}

View file

@ -0,0 +1,129 @@
{ lib, config, self, inputs, ... }:
let
inherit (import ../../../nix/utils.nix { inherit lib config self; })
eq
and
hasFiles;
in
let
createPackages = baseDir: { passthru ? { inherit inputs; }, ... }:
lib.pipe baseDir [
# Read given directory
builtins.readDir
# Map each entry to a package
(lib.mapAttrs'
(name: type:
let
packageDir = "${baseDir}/${name}";
systems = let
systemsPath = "${baseDir}/${name}/systems.nix";
in
# NOTE: If the package can restrict for which systems it wants to be built
if builtins.pathExists systemsPath
then import systemsPath
else lib.const true;
package = import packageDir;
result = {
inherit package systems;
};
in
if and [
(type == "directory")
(hasFiles [ "default.nix" ] (builtins.readDir packageDir))
] then
# NOTE: Classic package in a directory
lib.nameValuePair
name
result
else if and [
(type == "regular")
(lib.hasSuffix ".nix" name)
] then
# NOTE: Classic package in a file
lib.nameValuePair
(lib.removeSuffix ".nix" name)
result
else
# NOTE: Invalid package
lib.nameValuePair
name
null))
# Filter invalid packages
(lib.filterAttrs
(packageName: package:
package != null))
# Passthru if needed
(lib.mapAttrs
(packageName: package:
if and [
(builtins.isFunction
package)
(eq
(lib.pipe package [ builtins.functionArgs builtins.attrNames ])
(lib.pipe passthru [ builtins.attrNames ]))
]
then package passthru
else package))
];
in
{
options = let
inherit (lib) types;
in {
flake.autoPackages = lib.mkOption {
description = ''
Automagically generate packages from walking directories with Nix files
'';
type = types.submodule (submodule: {
options = {
enable = lib.mkEnableOption "Automatic packages extraction";
dir = lib.mkOption {
description = ''
Base directory of the contained packages
'';
type = types.path;
default = "${self}/pkgs";
defaultText = ''''${self}/pkgs'';
};
result = lib.mkOption {
description = ''
The resulting automatic packages
'';
type = types.attrsOf (types.submodule { options = {
package = lib.mkOption { type = types.unspecified; };
systems = lib.mkOption { type = types.functionTo types.bool; };
};});
readOnly = true;
internal = true;
default =
lib.optionalAttrs
config.flake.autoPackages.enable
(createPackages config.flake.autoPackages.dir { });
};
};
});
default = {};
};
};
config = {
perSystem = { lib, pkgs, system, ... }: let
packages =
lib.pipe
config.flake.autoPackages.result
[
(lib.filterAttrs
(name: { package, systems }:
pkgs.callPackage systems {
inherit (pkgs) lib hostPlatform targetPlatform;
}))
(lib.mapAttrs
(name: { package, systems }:
pkgs.callPackage package { }))
];
in {
inherit packages;
};
};
}

View file

@ -4,6 +4,8 @@
perSystem = { system, ... }: {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
# WARN: not including `self.packages` overylay
# because it causes an infinite recursion
overlays = lib.attrValues self.overlays ++ [
inputs.neovim-nightly-overlay.overlays.default
inputs.zig-overlay.overlays.default