-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Create bore nixos module #472353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zSuperx
wants to merge
3
commits into
NixOS:master
Choose a base branch
from
zSuperx:create-bore-nixos-module
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create bore nixos module #472353
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| { | ||
| config, | ||
| pkgs, | ||
| lib, | ||
| ... | ||
| }: | ||
| let | ||
| cfg = config.services.bore; | ||
| inherit (lib) | ||
| mkOption | ||
| mkPackageOption | ||
| mkEnableOption | ||
| types | ||
| optionalString | ||
| ; | ||
| inherit (types) attrsOf submodule; | ||
| in | ||
| { | ||
| options = { | ||
| services.bore = { | ||
| package = mkPackageOption pkgs "bore-cli" { }; | ||
|
|
||
| local = mkOption { | ||
| type = attrsOf (submodule ./local-options.nix); | ||
| description = "Definition of bore local proxies."; | ||
| default = { }; | ||
| }; | ||
|
|
||
| server = mkOption { | ||
| type = attrsOf (submodule ./server-options.nix); | ||
| description = "Definition of bore remote proxy servers."; | ||
| default = { }; | ||
| }; | ||
|
|
||
| enableLocalPortCheck = mkEnableOption "local port check" // { | ||
| default = true; | ||
| example = false; | ||
| }; | ||
|
|
||
| enableServerPortCheck = mkEnableOption "server port check" // { | ||
| default = true; | ||
| example = false; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| config = | ||
| let | ||
| getEnabled = s: builtins.filter (value: value.enable) (lib.attrValues s); | ||
| mkBoreService = | ||
| kind: v: | ||
| let | ||
| isServer = (kind == "server"); | ||
| in | ||
| { | ||
| name = "bore-${kind}-${v.name}"; | ||
| value = { | ||
| description = "bore remote proxy service for ${v.name}"; | ||
| enable = true; | ||
| after = [ | ||
| "network-online.target" | ||
| "nss-lookup.target" | ||
| ]; | ||
|
|
||
| requires = [ | ||
| "network-online.target" | ||
| "nss-lookup.target" | ||
| ]; | ||
|
|
||
| wantedBy = [ "multi-user.target" ]; | ||
|
|
||
| environment = | ||
| if isServer then | ||
| { | ||
| BORE_MIN_PORT = toString v.min-port; | ||
| BORE_MAX_PORT = toString v.max-port; | ||
| } | ||
| else | ||
| { | ||
| BORE_SERVER = v.to; | ||
| BORE_LOCAL_PORT = toString v.local-port; | ||
| }; | ||
|
|
||
| script = '' | ||
| ${optionalString (v.secretFile != null) ''export BORE_SECRET="$(<${v.secretFile})"''} | ||
|
|
||
| ${lib.getExe' cfg.package "bore"} ${kind} \ | ||
| '' | ||
| + ( | ||
| if isServer then | ||
| ''--bind-addr="${v.bind-addr}" --bind-tunnels="${v.bind-tunnels}"'' | ||
| else | ||
| ''--local-host="${v.local-host}" --port=${toString v.remote-port}'' | ||
| ); | ||
|
|
||
| serviceConfig = { | ||
| Restart = "on-failure"; | ||
| RestartSec = 10; | ||
| }; | ||
| }; | ||
| }; | ||
| in | ||
| { | ||
| systemd.services = | ||
| (lib.genAttrs' (getEnabled cfg.server) (mkBoreService "server")) | ||
| // (lib.genAttrs' (getEnabled cfg.local) (mkBoreService "local")); | ||
|
|
||
| assertions = | ||
| let | ||
| localTuples = map (v: "${v.local-host}:${toString v.local-port}") (getEnabled cfg.local); | ||
|
|
||
| # For remote tuples, filter out entries where remote-port == 0, since that's a legal default | ||
| remoteTuples = map (v: "${v.to}:${toString v.remote-port}") ( | ||
| builtins.filter (v: v.remote-port != 0) (getEnabled cfg.local) | ||
| ); | ||
|
|
||
| bindAddrs = map (v: v.bind-addr) (getEnabled cfg.server); | ||
|
|
||
| findDuplicates = | ||
| list: | ||
| lib.pipe list [ | ||
| (builtins.groupBy toString) | ||
| (builtins.mapAttrs (_: builtins.length)) | ||
| (lib.filterAttrs (_: len: len > 1)) | ||
| builtins.attrNames | ||
| ]; | ||
| in | ||
| [ | ||
| (lib.mkIf (cfg.enableLocalPortCheck) { | ||
| assertion = lib.allUnique localTuples; | ||
| message = '' | ||
| Detected duplicate values of the following `local-host:local-port` tuples: | ||
| ${lib.concatStringsSep ", " (findDuplicates localTuples)} | ||
| Ensure these tuples are unique across attributes in `services.bore.local`! | ||
| (To turn off this assertion, set `services.bore.enableLocalPortCheck = false;`) | ||
| ''; | ||
| }) | ||
|
|
||
| (lib.mkIf (cfg.enableLocalPortCheck) { | ||
| assertion = lib.allUnique remoteTuples; | ||
| message = '' | ||
| Detected duplicate values for `to:remote-port` tuples: | ||
| ${lib.concatStringsSep ", " (findDuplicates remoteTuples)} | ||
| Ensure these tuples are unique across attributes in `services.bore.local`! | ||
| (To turn off this assertion, set `services.bore.enableLocalPortCheck = false;`) | ||
| ''; | ||
| }) | ||
|
|
||
| (lib.mkIf (cfg.enableServerPortCheck) { | ||
| assertion = lib.allUnique bindAddrs; | ||
| message = '' | ||
| Detected duplicate values for bore remote `bind-addr`s: | ||
| ${lib.concatStringsSep ", " (findDuplicates bindAddrs)} | ||
| Ensure `bind-addr` is unique across attributes in `services.bore.server`! | ||
| (To turn off this assertion, set `services.bore.enableServerPortCheck = false;`) | ||
| ''; | ||
| }) | ||
| ]; | ||
| }; | ||
|
|
||
| meta.maintainers = with lib.maintainers; [ zsuper ]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| name, | ||
| lib, | ||
| ... | ||
| }: | ||
| let | ||
| inherit (lib) mkOption types literalMD; | ||
| inherit (types) nullOr str path; | ||
| in | ||
| { | ||
| options = { | ||
|
|
||
| name = mkOption { | ||
| type = str; | ||
| default = name; | ||
| defaultText = literalMD "the attribute name"; | ||
| description = "name of the service"; | ||
| }; | ||
|
|
||
| secretFile = mkOption { | ||
| type = nullOr path; | ||
| default = null; | ||
| description = '' | ||
| Optional path to file containing secret for authentication. | ||
| ''; | ||
| }; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| { | ||
| lib, | ||
| ... | ||
| }: | ||
| let | ||
| inherit (lib) mkOption mkEnableOption types; | ||
| inherit (types) port str; | ||
| in | ||
| { | ||
| imports = [ | ||
| ./common-options.nix | ||
| ]; | ||
|
|
||
| options = { | ||
| enable = mkEnableOption "Bore TCP tunnel (local proxy to the remote server) for `<name>`"; | ||
|
|
||
| local-host = mkOption { | ||
| type = str; | ||
| default = "localhost"; | ||
| description = '' | ||
| The local host to expose. | ||
| ''; | ||
| }; | ||
|
|
||
| to = mkOption { | ||
| type = str; | ||
| default = "bore.pub"; | ||
| description = '' | ||
| Address of the remote server to expose local ports to. | ||
| ''; | ||
| }; | ||
|
|
||
| remote-port = mkOption { | ||
| type = port; | ||
| default = 0; | ||
| description = '' | ||
| Optional port on the remote server to select. | ||
| (If set to 0, a random available port will be assigned by the remote server.) | ||
| ''; | ||
| }; | ||
|
|
||
| local-port = mkOption { | ||
| type = port; | ||
| description = '' | ||
| The local port to expose. | ||
| ''; | ||
| }; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| { | ||
| lib, | ||
| config, | ||
| options, | ||
| ... | ||
| }: | ||
| let | ||
| inherit (lib) mkOption mkEnableOption types; | ||
| inherit (types) | ||
| port | ||
| str | ||
| ; | ||
| in | ||
| { | ||
| imports = [ | ||
| ./common-options.nix | ||
| ]; | ||
|
|
||
| options = { | ||
| enable = mkEnableOption "Bore TCP tunnel (remote proxy server) for `<name>`"; | ||
|
|
||
| min-port = mkOption { | ||
| type = port; | ||
| default = 1024; | ||
| description = '' | ||
| Minimum accepted TCP port number. | ||
| ''; | ||
| }; | ||
|
|
||
| max-port = mkOption { | ||
| type = port; | ||
| default = 65535; | ||
| description = '' | ||
| Maximum accepted TCP port number. | ||
| ''; | ||
| }; | ||
|
|
||
| bind-addr = mkOption { | ||
| type = str; | ||
| default = "0.0.0.0"; | ||
| description = '' | ||
| IP address to bind to, clients must reach this. | ||
| ''; | ||
| }; | ||
|
|
||
| bind-tunnels = mkOption { | ||
| type = str; | ||
| default = config.bind-addr; | ||
| defaultText = lib.literalExpression "config.${options.bind-addr}"; | ||
| description = '' | ||
| IP address where tunnels will listen on. | ||
| ''; | ||
| }; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| { | ||
| name = "bore"; | ||
|
|
||
| nodes = { | ||
| serverStandalone = { | ||
| services.bore = { | ||
| server = { | ||
| example.enable = true; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| bothStandard = { | ||
| services.bore = { | ||
| server.example.enable = true; | ||
|
|
||
| local.example = { | ||
| enable = true; | ||
| to = "0.0.0.0"; | ||
| local-port = 3000; | ||
| remote-port = 4000; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| bothWithSecret = | ||
| { pkgs, ... }: | ||
| { | ||
| services.bore = { | ||
| server.example = { | ||
| secretFile = pkgs.writeText "server-secret" "super-secret-message!"; | ||
| enable = true; | ||
| }; | ||
|
|
||
| local.example = { | ||
| enable = true; | ||
| to = "0.0.0.0"; | ||
| local-port = 3000; | ||
| remote-port = 4000; | ||
| secretFile = pkgs.writeText "local-secret" "super-secret-message!"; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| testScript = '' | ||
| with subtest("Standalone server"): | ||
| serverStandalone.wait_for_unit("bore-server-example.service") | ||
| serverStandalone.wait_for_open_port(7835, "0.0.0.0") | ||
|
|
||
| with subtest("Server & Proxy running together"): | ||
| bothStandard.wait_for_unit("bore-server-example.service") | ||
| bothStandard.wait_for_open_port(7835, "0.0.0.0") | ||
|
|
||
| bothStandard.wait_for_unit("bore-local-example.service") | ||
| bothStandard.wait_for_open_port(4000, "0.0.0.0") | ||
|
|
||
| with subtest("Server & Proxy + authentication secret"): | ||
| bothWithSecret.wait_for_unit("bore-server-example.service") | ||
| bothWithSecret.wait_for_open_port(7835, "0.0.0.0") | ||
|
|
||
| bothWithSecret.wait_for_unit("bore-local-example.service") | ||
| bothWithSecret.wait_for_open_port(4000, "0.0.0.0") | ||
| ''; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.