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

@ -150,17 +150,20 @@
./modules/flake/agenix.nix
./modules/flake/deploy.nix
./modules/flake/topology
./modules/flake/packages
];
perSystem = { lib, pkgs, system, ... }: {
# Packages (`nix build`)
packages = import ./pkgs { inherit pkgs; };
perSystem = { lib, pkgs, system, ... }: {
# Apps (`nix run`)
apps = import ./apps { inherit pkgs; };
# Dev Shells (`nix develop`)
devShells = import ./shells { inherit pkgs inputs; };
devShells = import ./shells {
inherit inputs;
# NOTE: for `nixVersions.monitored`
pkgs = pkgs.extend inputs.self.overlays.modifications;
};
# Formatter (`nix fmt`)
formatter = pkgs.nixpkgs-fmt;
@ -175,6 +178,9 @@
# Automatic configurations, see `./modules/flake/configurations.nix`
autoConfigurations.enableAll = true;
# Automatic packages, see `./modules/flake/packages/default.nix`
autoPackages.enable = true;
# Templates
templates = import ./templates {
inherit inputs;

View file

@ -1,11 +1,11 @@
# This is your home-manager configuration file
# Use this to configure your home environment (it replaces ~/.config/nixpkgs/home.nix)
{ inputs, outputs, lib, pkgs, config, ... }: {
{ inputs, lib, pkgs, config, ... }: {
# You can import other home-manager modules here
imports = [
# If you want to use modules your own flake exports (from modules/home-manager):
# outputs.homeManagerModules.example
# inputs.self.homeManagerModules.example
# Or modules exported from other flakes (such as nix-colors):
# inputs.nix-colors.homeManagerModules.default

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [ ];

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
home = {

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [ ];

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
home = {

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [ ];

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
home = {

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.packages = with pkgs; [ ];

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
# Home Manager needs a bit of information about you and the

View file

@ -1,11 +1,11 @@
# This is your system's configuration file.
# Use this to configure your system environment (it replaces /etc/nixos/configuration.nix)
{ inputs, outputs, lib, pkgs, config, ... }: {
{ inputs, lib, pkgs, config, ... }: {
# You can import other NixOS modules here
imports = [
# If you want to use modules your own flake exports (from modules/nixos):
# outputs.nixosModules.example
# inputs.self.nixosModules.example
# Or modules from other flakes (such as nixos-hardware):
# inputs.hardware.nixosModules.common-cpu-amd
@ -22,8 +22,8 @@
# You can add overlays here
overlays = [
# If you want to use overlays your own flake exports (from overlays dir):
# outputs.overlays.modifications
# outputs.overlays.additions
# inputs.self.overlays.modifications
# inputs.self.overlays.additions
# Or overlays exported from other flakes:
# neovim-nightly-overlay.overlays.default

View file

@ -2,7 +2,7 @@
# your system. Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running nixos-help).
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
imports = [

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
imports = [

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
imports = [
inputs.hardware.nixosModules.common-cpu-amd

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
imports = [
inputs.disko.nixosModules.disko

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [
];

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
imports = [

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [
];

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
# age.secrets."nextcloud.adminpass" = {

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
networking.firewall.allowedTCPPorts = [11434];

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [
wireguard-tools

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

View file

@ -1,4 +1,4 @@
{ inputs, outputs, ... }:
{ inputs, ... }:
{ lib, pkgs, config, ... }:
{

View file

@ -1,4 +1,4 @@
{ inputs, outputs, ... }:
{ inputs, ... }:
{ lib, pkgs, config, ... }:
{
@ -22,7 +22,7 @@
#
# # Use flake overlays by default
# nixpkgs = {
# overlays = lib.attrValues outputs.overlays;
# overlays = lib.attrValues inputs.self.overlays;
#
# config.allowUnfree = true;
# };

View file

@ -1,4 +1,4 @@
{ inputs, outputs, ... }:
{ inputs, ... }:
{ lib, pkgs, config, ... }:
let

View file

@ -1,4 +1,4 @@
{ inputs, outputs, ... }:
{ inputs, ... }:
{ lib, pkgs, config, ... }:
with lib;

View file

@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, options, ... }:
{ inputs, lib, pkgs, config, options, ... }:
{
config = {
# NOTE: `(r)agenix` and `agenix-rekey` modules are imported by `../../../modules/flake/configurations.nix`

View file

@ -1,4 +1,4 @@
{ lib, self, ... }:
{ lib, config, self, ... }:
rec {
# Boolean helpers
@ -109,10 +109,7 @@ rec {
(lib.genAttrs
configuration-types
(configuration-type:
let
hosts = configuration-type-to-outputs-hosts configuration-type;
in
self.${hosts}))
config.flake.autoConfigurations.${configuration-type}.resultHosts))
(configuration-type: hosts:
lib.pipe
hosts

View file

@ -1,10 +1,9 @@
# This file defines overlays
{ inputs, ... }:
{
# This one brings our custom packages from the 'pkgs' directory
additions = final: _prev: import ../pkgs {
pkgs = final;
};
# This one brings our custom packages
# BUG: causes infinite recursion
# additions = final: prev: inputs.self.packages.${prev.system};
# This one contains whatever you want to overlay
# You can change versions, add patches, set compilation flags, anything really.
@ -30,7 +29,7 @@
};
};
nix-monitored = inputs.nix-monitored.packages.${final.system}.default.override {
nix-monitored = inputs.nix-monitored.packages.${prev.system}.default.override {
nix = prev.nix;
nix-output-monitor = prev.nix-output-monitor;
};

View file

@ -1,23 +0,0 @@
# Custom packages, that can be defined similarly to ones from nixpkgs
# You can build them using 'nix build .#example' or (legacy) 'nix-build -A example'
{ pkgs ? (import ../nixpkgs.nix) { }
, ...
}: {
# example = pkgs.callPackage ./example { };
advcpmv = pkgs.callPackage ./advcpmv { };
circom = pkgs.callPackage ./circom { };
circom-lsp = pkgs.callPackage ./circom-lsp { };
envsub = pkgs.callPackage ./envsub { };
fennel-language-server = pkgs.callPackage ./fennel-language-server { };
flamelens = pkgs.callPackage ./flamelens { };
parinfer-rust = pkgs.callPackage ./parinfer-rust { };
pest-ide-tools = pkgs.callPackage ./pest-ide-tools { };
# FIXME: only buildable on darwin
# pngpaste = pkgs.callPackage ./pngpaste { };
srtool-cli = pkgs.callPackage ./srtool-cli { };
# FIXME: does not build
# swww = pkgs.callPackage ./swww { };
vim-fmi-cli = pkgs.callPackage ./vim-fmi-cli { };
win2xcur = pkgs.callPackage ./win2xcur { };
}

View file

@ -0,0 +1,2 @@
{ lib, hostPlatform, targetPlatform, ... }:
targetPlatform.isDarwin

3
pkgs/swww/systems.nix Normal file
View file

@ -0,0 +1,3 @@
{ lib, hostPlatform, targetPlatform, ... }:
# BUG: does not build
false