Skip to content

Commit

Permalink
Merge pull request #20 from the-computer-club/flake-guard-self-error
Browse files Browse the repository at this point in the history
Option depreciations
  • Loading branch information
Skarlett authored Jun 20, 2024
2 parents 8f79660 + 356b3b7 commit 28c010a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 24 deletions.
30 changes: 30 additions & 0 deletions flake-modules/builtins/assertions.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ lib, ... }:
with lib;
{
options = {
assertions = mkOption {
type = types.listOf types.unspecified;
internal = true;
default = [];
example = [ { assertion = false; message = "you can't enable this for that reason"; } ];
description = ''
This option allows modules to express conditions that must
hold for the evaluation of the system configuration to
succeed, along with associated error messages for the user.
'';
};

warnings = mkOption {
internal = true;
default = [];
type = types.listOf types.str;
example = [ "The `foo' service is deprecated and will go away soon!" ];
description = ''
This option allows modules to show warnings to users during
the evaluation of the system configuration.
'';
};
};

# impl of this is in lib.nix:evalFlakeModules
}
4 changes: 3 additions & 1 deletion flake-modules/domains/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
inputs
, config
, stdlib
, lib
, lynxlib
, flake-parts-lib
, ...
}:
Expand Down Expand Up @@ -53,7 +55,7 @@
};

config.build.domains = builtins.mapAttrs(domain: toplevel:
(flake-parts-lib.evalFlakeModule {
(lynxlib.evalFlakeModuleWithAssertions {
inherit inputs;
inherit (toplevel) specialArgs;
} { imports = toplevel.modules; })
Expand Down
32 changes: 21 additions & 11 deletions flake-modules/flake-guard/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let
mkOption
mkEnableOption
mkIf
mkRemovedOptionModule
types
optionalString
optionals
Expand All @@ -20,7 +21,17 @@ let
;
in
{
imports = [ ./options.nix ];
imports = [
(mkRemovedOptionModule [ "wireguard" "enable" ] ''
wireguard.enable was removed because it often causes user errors
where `wireguard.enable` was set to `false` but users had enabled
the nixos options `autoConfig.interface`.
This lead to errors messages which were hard to understand.
'')

./options.nix
];


flake.nixosModules.flake-guard-host = {config, ...}:
let cfg = config.networking.wireguard.networks;
Expand All @@ -30,7 +41,6 @@ in
default = {};
type = types.attrsOf (types.submodule {
options = {

autoConfig = {
interface = mkEnableOption "automatically generate the underlying network interface";
peers = mkEnableOption "automatically generate the peers -- this will add all peers in the network to the interface.";
Expand Down Expand Up @@ -65,12 +75,9 @@ in
});
};

config = mkIf rootConfig.enable
{

networking.wireguard.networks = mapAttrs (net-name: network:
config.networking.wireguard.networks =
(mapAttrs (net-name: network:
let

self-name = builtins.head
(builtins.filter (x: x == config.networking.hostName)
(builtins.attrNames network.peers.by-name));
Expand Down Expand Up @@ -108,10 +115,11 @@ in
inherit self;
peers.by-name = mapAttrs (pname: peer: (toPeer peer)) network.peers.by-name;
peers.list = map toPeer (builtins.attrValues network.peers.by-name);
}) rootConfig.networks;
}) rootConfig.networks);

networking.wireguard.interfaces = mapAttrs (net-name: network:
mkIf network.autoConfig.interface {
config.networking.wireguard.interfaces = mapAttrs (net-name: network:
mkIf network.autoConfig.interface
{
inherit (config.networking.wireguard.networks.${net-name}.self)
listenPort
privateKeyFile
Expand All @@ -123,6 +131,8 @@ in
);
})
config.networking.wireguard.networks;
};
};



}
23 changes: 11 additions & 12 deletions flake-modules/flake-guard/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ inherit (lib)
in
{
options.wireguard = {
enable = mkEnableOption "Enable wireguard";

enable = mkEnableOption "depreciated";
networks = mkOption {
type = types.attrsOf (types.submodule {
options = {
Expand Down Expand Up @@ -132,14 +131,14 @@ in
};

config.wireguard.build.networks =
mapAttrs (net-name: network:
{
peers.by-name = mapAttrs (peer-name: peer:
peer // {
sopsLookup = if peer.sopsLookup != null
then peer.sopsLookup
else network.sopsLookup;
}
) network.peers.by-name;
}) config.wireguard.networks;
(mapAttrs (net-name: network:
{
peers.by-name = mapAttrs (peer-name: peer:
peer // {
sopsLookup = if peer.sopsLookup != null
then peer.sopsLookup
else network.sopsLookup;
}
) network.peers.by-name;
}) config.wireguard.networks);
}
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
reuse-password-prompt = import ./nixos-modules/fs/zfs/reuse-password-prompt.nix;
};
};

lib = import ./lib.nix;
};
}
39 changes: 39 additions & 0 deletions lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{ flake-parts-lib, ... }:
with builtins;
let
inherit (flake-parts-lib) evalFlakeModule;

singleModuleBase = x: {
imports = [
./flake-modules/builtins/assertions.nix
x
];
};

evalAssertions = eval:
let
failedAssertions = map (x: x.message) (filter (x: !x.assertion) eval.config.assertions);
warnings = eval.config.warnings;
in
if (failedAssertions != [])
then
builtins.abort (concatStringsSep "\n\n" failedAssertions)
else
if (warnings != [])
then
builtins.trace (concatStringsSep "\n\n" warnings)
eval
else eval;


evalFlakeModuleWithAssertions = a: m:
evalAssertions (evalFlakeModule a (singleModuleBase m));
in
{
inherit evalFlakeModuleWithAssertions;
mkFlakeWithAssertions = args: module:
let
eval = evalFlakeModuleWithAssertions args module;
in
eval.config.flake;
}

0 comments on commit 28c010a

Please sign in to comment.