-
-
Notifications
You must be signed in to change notification settings - Fork 17.7k
nixos/nemorosa: init service #462968
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
undefined-landmark
wants to merge
2
commits into
NixOS:master
Choose a base branch
from
undefined-landmark:nemorosa-service
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
nixos/nemorosa: init service #462968
Changes from all commits
Commits
Show all changes
2 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,185 @@ | ||
| { | ||
| config, | ||
| lib, | ||
| pkgs, | ||
| utils, | ||
| maintainers, | ||
| ... | ||
| }: | ||
| let | ||
| cfg = config.services.nemorosa; | ||
|
|
||
| inherit (lib) | ||
| getExe | ||
| mkEnableOption | ||
| mkIf | ||
| mkOption | ||
| mkPackageOption | ||
| ; | ||
| inherit (lib.types) | ||
| port | ||
| str | ||
| submodule | ||
| ; | ||
|
|
||
| settingsFormat = pkgs.formats.yaml { }; | ||
| stateDir = "/var/lib/nemorosa"; | ||
| configPath = "${stateDir}/config.yaml"; | ||
| # YAML is a JSON superset | ||
| secretsReplacement = utils.genJqSecretsReplacement { | ||
| loadCredential = true; | ||
| } cfg.settings configPath; | ||
| in | ||
| { | ||
| options.services.nemorosa = { | ||
| enable = mkEnableOption "nemorosa"; | ||
|
|
||
| package = mkPackageOption pkgs "nemorosa" { }; | ||
|
|
||
| user = mkOption { | ||
| type = str; | ||
| default = "nemorosa"; | ||
| description = "User to run nemorosa as."; | ||
| }; | ||
|
|
||
| group = mkOption { | ||
| type = str; | ||
| default = "nemorosa"; | ||
| description = "Group to run nemorosa as."; | ||
| }; | ||
|
|
||
| settings = mkOption { | ||
| description = '' | ||
| Configuration options for nemorosa. | ||
| See [the nemorosa documentation](https://github.com/KyokoMiki/nemorosa/wiki/Configuration) | ||
| for all configuration options. | ||
|
|
||
| The configuration is processed using [utils.genJqSecretsReplacement](https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/utils.nix#L232-L331) to handle secret substitution. | ||
| Use this for sensitive configuration options. | ||
| ''; | ||
| default = { }; | ||
| example = { | ||
| linking = { | ||
| enable_linking = true; | ||
| link_dirs = [ "/mnt/some/folder" ]; | ||
| }; | ||
| downloader = { | ||
| client = { | ||
| _secret = "/run/secrets/torrent-client-url"; | ||
| }; | ||
| }; | ||
| target_site = [ | ||
| { | ||
| server = "https://someurl.com"; | ||
| api_key = { | ||
| _secret = "/run/secrets/someurl_api-key"; | ||
| }; | ||
| } | ||
| ]; | ||
| }; | ||
| type = submodule { | ||
| freeformType = settingsFormat.type; | ||
| options = { | ||
| server = mkOption { | ||
| default = { }; | ||
| description = "Web server configuration"; | ||
| type = submodule { | ||
| freeformType = settingsFormat.type; | ||
| options = { | ||
| host = mkOption { | ||
| type = str; | ||
| default = "127.0.0.1"; | ||
| description = "Host address the nemorosa daemon listens on."; | ||
| }; | ||
| port = mkOption { | ||
| type = port; | ||
| default = 8256; | ||
| description = "Port the nemorosa daemon listens on."; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| config = mkIf (cfg.enable) { | ||
| systemd = { | ||
| services.nemorosa = { | ||
| description = "nemorosa"; | ||
| after = [ "network-online.target" ]; | ||
| wants = [ "network-online.target" ]; | ||
| wantedBy = [ "multi-user.target" ]; | ||
|
|
||
| preStart = secretsReplacement.script; | ||
|
|
||
| serviceConfig = { | ||
| Type = "simple"; | ||
| User = cfg.user; | ||
| Group = cfg.group; | ||
| # On partial matches the torrent client also needs to write to the | ||
| # link_dirs. With default 755/644 permissions this is not possible, | ||
| # unless the user is the same as the torrent client user. | ||
| UMask = mkIf (cfg.user == "nemorosa") "0002"; | ||
| StateDirectory = "nemorosa"; | ||
| LoadCredential = secretsReplacement.credentials; | ||
|
|
||
| ExecStart = "${getExe cfg.package} --server --config ${configPath}"; | ||
|
|
||
| SocketBindDeny = "any"; | ||
| SocketBindAllow = cfg.settings.server.port; | ||
|
|
||
| # Based the qbittorrent hardening settings | ||
| CapabilityBoundingSet = ""; | ||
| LockPersonality = true; | ||
| MemoryDenyWriteExecute = true; | ||
| NoNewPrivileges = true; | ||
| PrivateDevices = true; | ||
| PrivateNetwork = false; | ||
| PrivateTmp = true; | ||
| PrivateUsers = true; | ||
| ProcSubset = "pid"; | ||
| ProtectClock = true; | ||
| ProtectControlGroups = true; | ||
| ProtectHome = "yes"; | ||
| ProtectHostname = true; | ||
| ProtectKernelLogs = true; | ||
| ProtectKernelModules = true; | ||
| ProtectKernelTunables = true; | ||
| ProtectProc = "invisible"; | ||
| ProtectSystem = "full"; | ||
| RemoveIPC = true; | ||
| RestrictAddressFamilies = [ | ||
| "AF_INET" | ||
| "AF_INET6" | ||
| "AF_NETLINK" | ||
| "AF_UNIX" | ||
| ]; | ||
| RestrictNamespaces = true; | ||
| RestrictRealtime = true; | ||
| RestrictSUIDSGID = true; | ||
| SystemCallArchitectures = "native"; | ||
| SystemCallFilter = [ "@system-service" ]; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| users = { | ||
| users = mkIf (cfg.user == "nemorosa") { | ||
| nemorosa = { | ||
| group = cfg.group; | ||
| description = "nemorosa user"; | ||
| isSystemUser = true; | ||
| home = stateDir; | ||
| }; | ||
| }; | ||
|
|
||
| groups = mkIf (cfg.group == "nemorosa") { | ||
| nemorosa = { }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| meta.maintainers = with maintainers; [ undefined-landmark ]; | ||
| } | ||
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
undefined-landmark marked this conversation as resolved.
Show resolved
Hide resolved
|
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,68 @@ | ||
| { lib, pkgs, ... }: | ||
| { | ||
| name = "nemorosa"; | ||
|
|
||
| meta = with pkgs.lib.maintainers; { | ||
| maintainers = [ | ||
| undefined-landmark | ||
| ]; | ||
| }; | ||
|
|
||
| nodes.machine = | ||
| let | ||
| qbitUrl = "qbittorrent+http://user:adminadmin@localhost:8080/"; | ||
| # We create this secret in the Nix store (making it readable by everyone). | ||
| # DO NOT DO THIS OUTSIDE OF TESTS!! | ||
| testSecret = pkgs.writeText "qbitUrl" qbitUrl; | ||
| in | ||
| { | ||
| services.qbittorrent = { | ||
| enable = true; | ||
| serverConfig.Preferences.WebUI = { | ||
| Username = "user"; | ||
| # password: "adminadmin" as ByteArray | ||
| Password_PBKDF2 = "@ByteArray(6DIf26VOpTCYbgNiO6DAFQ==:e6241eaAWGzRotQZvVA5/up9fj5wwSAThLgXI2lVMsYTu1StUgX9MgmElU3Sa/M8fs+zqwZv9URiUOObjqJGNw==)"; | ||
| }; | ||
| }; | ||
|
|
||
| services.nemorosa = { | ||
| enable = true; | ||
| settings.downloader.client = lib.mkDefault qbitUrl; | ||
| }; | ||
|
|
||
| specialisation.secretSubstition.configuration = { | ||
| services.nemorosa = { | ||
| enable = true; | ||
| settings = { | ||
| downloader.client = { | ||
| _secret = toString testSecret; | ||
| }; | ||
| server.port = 8266; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| testScript = | ||
| { nodes, ... }: | ||
| let | ||
| secretSubst = "${nodes.machine.system.build.toplevel}/specialisation/secretSubstition"; | ||
| in | ||
| # python | ||
| '' | ||
| machine.start() | ||
|
|
||
| # Nemorosa initialization expects an available API connection. So the | ||
| # service will not start successfully. Do some basic checks on the | ||
| # console log as an alternative. | ||
| def test_log(port): | ||
| machine.wait_for_console_text("Configuration loaded successfully from: /var/lib/nemorosa/config.yaml") | ||
| machine.wait_for_console_text(f"Starting Nemorosa web server on 127.0.0.1:{port}") | ||
| machine.wait_for_console_text("Successfully connected to torrent client") | ||
|
|
||
| test_log(8256) | ||
|
|
||
| machine.succeed("${secretSubst}/bin/switch-to-configuration test") | ||
| test_log(8266) | ||
| ''; | ||
| } |
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
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.