Skip to content

Commit

Permalink
refactor(flake)!: export packages using a flake module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
reo101 committed Jul 28, 2024
1 parent e45db02 commit 2941536
Show file tree
Hide file tree
Showing 36 changed files with 241 additions and 118 deletions.
14 changes: 10 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions hosts/home-manager/x86_64-linux/__template__/home.nix
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion hosts/nix-darwin/aarch64-darwin/limonka/configuration.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
environment.systemPackages = with pkgs; [ ];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
home = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
environment.systemPackages = with pkgs; [ ];
Expand Down
2 changes: 1 addition & 1 deletion hosts/nix-darwin/x86_64-darwin/apavel-a01/home/apavel.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
home = {
Expand Down
2 changes: 1 addition & 1 deletion hosts/nix-darwin/x86_64-darwin/limontozu/configuration.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
environment.systemPackages = with pkgs; [ ];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
home = {
Expand Down
2 changes: 1 addition & 1 deletion hosts/nix-on-droid/aarch64-linux/cheetah/configuration.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
environment.packages = with pkgs; [ ];
Expand Down
2 changes: 1 addition & 1 deletion hosts/nix-on-droid/aarch64-linux/cheetah/home.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
# Home Manager needs a bit of information about you and the
Expand Down
8 changes: 4 additions & 4 deletions hosts/nixos/x86_64-linux/__template__/configuration.nix
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/homix/configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/homix/home/reo101.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
imports = [
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/configuration.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
imports = [
inputs.hardware.nixosModules.common-cpu-amd
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/disko.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
imports = [
inputs.disko.nixosModules.disko
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/home-assistant/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [
];
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/home/jeeves.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
imports = [
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/network.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [
];
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/nginx.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:

{
# age.secrets."nextcloud.adminpass" = {
Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/ollama.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
networking.firewall.allowedTCPPorts = [11434];

Expand Down
2 changes: 1 addition & 1 deletion hosts/nixos/x86_64-linux/jeeves/wireguard.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, outputs, lib, pkgs, config, ... }:
{ inputs, lib, pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [
wireguard-tools
Expand Down
68 changes: 36 additions & 32 deletions modules/flake/configurations.nix
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
};
Expand All @@ -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
Expand All @@ -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
Expand All @@ -79,15 +83,15 @@ 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;
};

mkNixDarwinHost = args @ { root, system, hostname, users }: inputs.nix-darwin.lib.darwinSystem {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
pkgs = pkgs' system;

modules = [
# Main configuration
Expand All @@ -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;
};
};
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -288,7 +292,7 @@ in
inherit system;
hostname = host;
})
self.${configuration-type-to-outputs-hosts "home-manager"};
config.flake.autoConfigurations.home-manager.resultHosts;
};
};
}
2 changes: 1 addition & 1 deletion modules/flake/deploy.nix
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading

0 comments on commit 2941536

Please sign in to comment.