diff options
Diffstat (limited to 'nixpkgs/nixos/modules/services/networking/nbd.nix')
-rw-r--r-- | nixpkgs/nixos/modules/services/networking/nbd.nix | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/nixpkgs/nixos/modules/services/networking/nbd.nix b/nixpkgs/nixos/modules/services/networking/nbd.nix index 87f8c41a8e5c..76ca11dfea76 100644 --- a/nixpkgs/nixos/modules/services/networking/nbd.nix +++ b/nixpkgs/nixos/modules/services/networking/nbd.nix @@ -4,28 +4,34 @@ with lib; let cfg = config.services.nbd; - configFormat = pkgs.formats.ini { }; iniFields = with types; attrsOf (oneOf [ bool int float str ]); - serverConfig = configFormat.generate "nbd-server-config" - ({ - generic = - (cfg.server.extraOptions // { - user = "root"; - group = "root"; - port = cfg.server.listenPort; - } // (optionalAttrs (cfg.server.listenAddress != null) { - listenaddr = cfg.server.listenAddress; - })); - } - // (mapAttrs + # The `[generic]` section must come before all the others in the + # config file. This means we can't just dump an attrset to INI + # because that sorts the sections by name. Instead, we serialize it + # on its own first. + genericSection = { + generic = (cfg.server.extraOptions // { + user = "root"; + group = "root"; + port = cfg.server.listenPort; + } // (optionalAttrs (cfg.server.listenAddress != null) { + listenaddr = cfg.server.listenAddress; + })); + }; + exportSections = + mapAttrs (_: { path, allowAddresses, extraOptions }: extraOptions // { exportname = path; } // (optionalAttrs (allowAddresses != null) { authfile = pkgs.writeText "authfile" (concatStringsSep "\n" allowAddresses); })) - cfg.server.exports) - ); + cfg.server.exports; + serverConfig = + pkgs.writeText "nbd-server-config" '' + ${lib.generators.toINI {} genericSection} + ${lib.generators.toINI {} exportSections} + ''; splitLists = partition (path: hasPrefix "/dev/" path) @@ -42,7 +48,7 @@ in listenPort = mkOption { type = types.port; default = 10809; - description = "Port to listen on. The port is NOT automatically opened in the firewall."; + description = lib.mdDoc "Port to listen on. The port is NOT automatically opened in the firewall."; }; extraOptions = mkOption { @@ -50,22 +56,21 @@ in default = { allowlist = false; }; - description = '' + description = lib.mdDoc '' Extra options for the server. See - <citerefentry><refentrytitle>nbd-server</refentrytitle> - <manvolnum>5</manvolnum></citerefentry>. + {manpage}`nbd-server(5)`. ''; }; exports = mkOption { - description = "Files or block devices to make available over the network."; + description = lib.mdDoc "Files or block devices to make available over the network."; default = { }; type = with types; attrsOf (submodule { options = { path = mkOption { type = str; - description = "File or block device to export."; + description = lib.mdDoc "File or block device to export."; example = "/dev/sdb1"; }; @@ -73,7 +78,7 @@ in type = nullOr (listOf str); default = null; example = [ "10.10.0.0/24" "127.0.0.1" ]; - description = "IPs and subnets that are authorized to connect for this device. If not specified, the server will allow all connections."; + description = lib.mdDoc "IPs and subnets that are authorized to connect for this device. If not specified, the server will allow all connections."; }; extraOptions = mkOption { @@ -82,10 +87,9 @@ in flush = true; fua = true; }; - description = '' + description = lib.mdDoc '' Extra options for this export. See - <citerefentry><refentrytitle>nbd-server</refentrytitle> - <manvolnum>5</manvolnum></citerefentry>. + {manpage}`nbd-server(5)`. ''; }; }; @@ -94,7 +98,7 @@ in listenAddress = mkOption { type = with types; nullOr str; - description = "Address to listen on. If not specified, the server will listen on all interfaces."; + description = lib.mdDoc "Address to listen on. If not specified, the server will listen on all interfaces."; default = null; example = "10.10.0.1"; }; @@ -103,6 +107,13 @@ in }; config = mkIf cfg.server.enable { + assertions = [ + { + assertion = !(cfg.server.exports ? "generic"); + message = "services.nbd.server exports must not be named 'generic'"; + } + ]; + boot.kernelModules = [ "nbd" ]; systemd.services.nbd-server = { |