Skip to content

Commit

Permalink
feat: Pass nix-config as self to avoid infinite recursion
Browse files Browse the repository at this point in the history
This change makes it possible to use this nix-config in all the
different ways imaginable (containers, bare metal, tests, and as a
separate flake input) *without* running into infinite recursion
issues with self.

It does this by using a trick similar to JavaScript in which
`var self = this;`, thus enabling the usage of "this" (or self, in
Nix's case) where it wouldn't otherwise be possible.

Note that this *only* works if the input for this repository is named
nix-config. This makes it impractical to combine with multiple
configurations that employ the same strategy.
  • Loading branch information
donovanglover committed Apr 5, 2024
1 parent b368817 commit 59f557a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
nixosConfigurations = {
nixos = nixosSystem {
system = "x86_64-linux";
specialArgs = attrs;
specialArgs = attrs // { nix-config = self; };
modules = [
./hardware/laptop.nix
{
Expand Down
5 changes: 2 additions & 3 deletions modules/containers.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, stylix, home-manager, sakaya, ... }:
{ config, nix-config, sakaya, ... }:

let
inherit (config.modules.system) username;
Expand Down Expand Up @@ -39,8 +39,7 @@ let
];

specialArgs = {
inherit home-manager;
inherit stylix;
inherit nix-config;
};
};
in
Expand Down
6 changes: 3 additions & 3 deletions modules/desktop.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ home-manager, stylix, pkgs, config, lib, ... }:
{ nix-config, pkgs, config, lib, ... }:

let
inherit (lib) mkEnableOption mkIf mkMerge;
Expand All @@ -14,8 +14,8 @@ let
in
{
imports = attrValues {
inherit (home-manager.nixosModules) home-manager;
inherit (stylix.nixosModules) stylix;
inherit (nix-config.inputs.home-manager.nixosModules) home-manager;
inherit (nix-config.inputs.stylix.nixosModules) stylix;
};

options.modules.desktop = {
Expand Down
7 changes: 6 additions & 1 deletion modules/system.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{ pkgs, lib, config, ... }:
{ nix-config, pkgs, lib, config, ... }:

let
inherit (lib) mkOption;
inherit (lib.types) str listOf;
inherit (pkgs.nixVersions) nix_2_19;
inherit (cfg) username;
inherit (builtins) attrValues;

cfg = config.modules.system;
in
{
imports = attrValues {
inherit (nix-config.inputs.home-manager.nixosModules) home-manager;
};

options.modules.system = {
username = mkOption {
type = str;
Expand Down
1 change: 1 addition & 0 deletions tests/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ in
defaults.documentation.enable = lib.mkDefault false;
node.specialArgs = {
inherit self;
nix-config = self;
};
imports = [ test ];
}).config.result

0 comments on commit 59f557a

Please sign in to comment.