Skip to content

Commit

Permalink
make options camel case
Browse files Browse the repository at this point in the history
`mkRenamedOptionModule` has been used to throw warnings if the old
option names are used, while still allowing for the old names to work.
A warning is only thrown for 'vpnnamespaces' as the warnings apparently
do not work for submodules. This might have something to do with
NixOS/nixpkgs#96006
  • Loading branch information
Maroka-chan committed Sep 8, 2024
1 parent e8ea929 commit 07b5600
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ A NixOS module which lets you route traffic from systemd services through a VPN
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
vpnconfinement.url = "github:Maroka-chan/VPN-Confinement";
vpnconfinement.inputs.nixpkgs.follows = "nixpkgs";
vpnConfinement.url = "github:Maroka-chan/VPN-Confinement";
vpnConfinement.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, vpnconfinement, ... }:
outputs = { self, nixpkgs, vpnConfinement, ... }:
{
# Change hostname, system, etc. as needed.
nixosConfigurations.hostname = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
vpnconfinement.nixosModules.default
vpnConfinement.nixosModules.default
];
};
};
Expand All @@ -33,7 +33,7 @@ A NixOS module which lets you route traffic from systemd services through a VPN
## Define VPN network namespace

```nix
vpnnamespaces.<name> = { # The name is limited to 7 characters
vpnNamespaces.<name> = { # The name is limited to 7 characters
enable = true;
wireguardConfigFile = <path to secret wireguard config file>;
accessibleFrom = [
Expand All @@ -54,9 +54,9 @@ vpnnamespaces.<name> = { # The name is limited to 7 characters
## Add systemd service to VPN network namespace

```nix
systemd.services.<name>.vpnconfinement = {
systemd.services.<name>.vpnConfinement = {
enable = true;
vpnnamespace = "<network namespace name>";
vpnNamespace = "<network namespace name>";
};
```

Expand All @@ -67,7 +67,7 @@ systemd.services.<name>.vpnconfinement = {
{ pkgs, lib, config, ... }:
{
# Define VPN network namespace
vpnnamespaces.wg = {
vpnNamespaces.wg = {
enable = true;
wireguardConfigFile = /. + "/secrets/wg0.conf";
accessibleFrom = [
Expand All @@ -83,9 +83,9 @@ systemd.services.<name>.vpnconfinement = {
};
# Add systemd service to VPN network namespace.
systemd.services.transmission.vpnconfinement = {
systemd.services.transmission.vpnConfinement = {
enable = true;
vpnnamespace = "wg";
vpnNamespace = "wg";
};
services.transmission = {
Expand Down
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

flake = {
nixosModules = rec {
vpnconfinement = ./modules/vpnnetns.nix;
default = vpnconfinement;
vpnConfinement = ./modules/vpnnetns.nix;
default = vpnConfinement;
};
};
};
Expand Down
13 changes: 9 additions & 4 deletions modules/systemd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ with lib;
{
options.systemd.services = mkOption {
type = types.attrsOf (types.submodule ({ name, config, ... }: {
options.vpnconfinement = {
options.vpnConfinement = {
enable = mkOption {
type = types.bool;
default = false;
Expand All @@ -13,7 +13,7 @@ with lib;
VPN tunnel and forces a specified DNS.
'';
};
vpnnamespace = mkOption {
vpnNamespace = mkOption {
type = types.str;
default = null;
example = "wg";
Expand All @@ -24,9 +24,14 @@ with lib;
};
};

imports = [
(mkRenamedOptionModule [ "vpnconfinement" "enable" ] [ "vpnConfinement" "enable" ])
(mkRenamedOptionModule [ "vpnconfinement" "vpnnamespace" ] [ "vpnConfinement" "vpnNamespace" ])
];

config = let
vpn = config.vpnconfinement.vpnnamespace;
in mkIf config.vpnconfinement.enable {
vpn = config.vpnConfinement.vpnNamespace;
in mkIf config.vpnConfinement.enable {
bindsTo = [ "${vpn}.service" ];
after = [ "${vpn}.service" ];

Expand Down
9 changes: 5 additions & 4 deletions modules/vpnnetns.nix
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,17 @@ let
};
};
in {
imports = [ ./systemd.nix ]; # Confinement options for systemd services
imports = [ ./systemd.nix ] # Confinement options for systemd services
++ [(mkRenamedOptionModule [ "vpnnamespaces" ] [ "vpnNamespaces" ])];

options.vpnnamespaces = mkOption {
options.vpnNamespaces = mkOption {
type = with types; attrsOf (submodule [ (import ./options.nix) ]);
default = {};
};

config = mkIf (config.vpnnamespaces != {}) {
config = mkIf (config.vpnNamespaces != {}) {
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
systemd.services = mapAttrs' (n: v: nameValuePair n (namespaceToService n v)) config.vpnnamespaces;
systemd.services = mapAttrs' (n: v: nameValuePair n (namespaceToService n v)) config.vpnNamespaces;
systemd.tmpfiles.rules = [ "d /var/run/resolvconf 0755 root root" ]; # Make sure resolvconf path exists
};
}
12 changes: 6 additions & 6 deletions tests/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
};
};
basicNetns = {
vpnnamespaces.wg = {
vpnNamespaces.wg = {
enable = true;
accessibleFrom = [
"192.168.0.0/24"
Expand All @@ -50,19 +50,19 @@
networking.dhcpcd.enable = false;
};
machine_max_name_length = { pkgs, ... }: base // {
vpnnamespaces.vpnname = {
vpnNamespaces.vpnname = {
enable = true;
wireguardConfigFile = "/etc/wireguard/wg0.conf";
};
};
machine_dash_in_name = { pkgs, ... }: base // {
vpnnamespaces.vpn-nam = {
vpnNamespaces.vpn-nam = {
enable = true;
wireguardConfigFile = "/etc/wireguard/wg0.conf";
};
};
machine_arbitrary_config_name = { pkgs, ... }: base // {
vpnnamespaces.vpn-nam = {
vpnNamespaces.vpn-nam = {
enable = true;
wireguardConfigFile = "/etc/wireguard/wireguardconfiguration.txt";
};
Expand All @@ -77,8 +77,8 @@
services.prowlarr.enable = true;

systemd.services.prowlarr = {
vpnconfinement.enable = true;
vpnconfinement.vpnnamespace = "wg";
vpnConfinement.enable = true;
vpnConfinement.vpnNamespace = "wg";
};
};
machine_no_namespaces = { pkgs, ... }: base // {
Expand Down

0 comments on commit 07b5600

Please sign in to comment.