summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/baget.nix
blob: 3007dd4fbb26780601528d2621fa1273a8803078 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.baget;

  defaultConfig = {
    "PackageDeletionBehavior" = "Unlist";
    "AllowPackageOverwrites" = false;

    "Database" = {
      "Type" = "Sqlite";
      "ConnectionString" = "Data Source=baget.db";
    };

    "Storage" = {
      "Type" = "FileSystem";
      "Path" = "";
    };

    "Search" = {
      "Type" = "Database";
    };

    "Mirror" = {
      "Enabled" = false;
      "PackageSource" = "https://api.nuget.org/v3/index.json";
    };

    "Logging" = {
      "IncludeScopes" = false;
      "Debug" = {
        "LogLevel" = {
          "Default" = "Warning";
        };
      };
      "Console" = {
        "LogLevel" = {
          "Microsoft.Hosting.Lifetime" = "Information";
          "Default" = "Warning";
        };
      };
    };
  };

  configAttrs = recursiveUpdate defaultConfig cfg.extraConfig;

  configFormat = pkgs.formats.json {};
  configFile = configFormat.generate "appsettings.json" configAttrs;

in
{
  options.services.baget = {
    enable = mkEnableOption "BaGet NuGet-compatible server";

    apiKeyFile = mkOption {
      type = types.path;
      example = "/root/baget.key";
      description = ''
        Private API key for BaGet.
      '';
    };

    extraConfig = mkOption {
      type = configFormat.type;
      default = {};
      example = {
        "Database" = {
          "Type" = "PostgreSql";
          "ConnectionString" = "Server=/run/postgresql;Port=5432;";
        };
      };
      defaultText = literalExpression ''
        {
          "PackageDeletionBehavior" = "Unlist";
          "AllowPackageOverwrites" = false;

          "Database" = {
            "Type" = "Sqlite";
            "ConnectionString" = "Data Source=baget.db";
          };

          "Storage" = {
            "Type" = "FileSystem";
            "Path" = "";
          };

          "Search" = {
            "Type" = "Database";
          };

          "Mirror" = {
            "Enabled" = false;
            "PackageSource" = "https://api.nuget.org/v3/index.json";
          };

          "Logging" = {
            "IncludeScopes" = false;
            "Debug" = {
              "LogLevel" = {
                "Default" = "Warning";
              };
            };
            "Console" = {
              "LogLevel" = {
                "Microsoft.Hosting.Lifetime" = "Information";
                "Default" = "Warning";
              };
            };
          };
        }
      '';
      description = ''
        Extra configuration options for BaGet. Refer to <link xlink:href="https://loic-sharma.github.io/BaGet/configuration/"/> for details.
        Default value is merged with values from here.
      '';
    };
  };

  # implementation

  config = mkIf cfg.enable {

    systemd.services.baget = {
      description = "BaGet server";
      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
      after = [ "network.target" "network-online.target" ];
      path = [ pkgs.jq ];
      serviceConfig = {
        WorkingDirectory = "/var/lib/baget";
        DynamicUser = true;
        StateDirectory = "baget";
        StateDirectoryMode = "0700";
        LoadCredential = "api_key:${cfg.apiKeyFile}";

        CapabilityBoundingSet = "";
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = true;
        PrivateUsers = true;
        PrivateMounts = true;
        ProtectHome = true;
        ProtectClock = true;
        ProtectProc = "noaccess";
        ProcSubset = "pid";
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        ProtectHostname = true;
        RestrictSUIDSGID = true;
        RestrictRealtime = true;
        RestrictNamespaces = true;
        LockPersonality = true;
        RemoveIPC = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        SystemCallFilter = [ "@system-service" "~@privileged" ];
      };
      script = ''
        jq --slurpfile apiKeys <(jq -R . "$CREDENTIALS_DIRECTORY/api_key") '.ApiKey = $apiKeys[0]' ${configFile} > appsettings.json
        ln -snf ${pkgs.baget}/lib/BaGet/wwwroot wwwroot
        exec ${pkgs.baget}/bin/BaGet
      '';
    };

  };
}