about summary refs log tree commit diff stats
path: root/modules/by-name/ni/nix-sync/module.nix
blob: 9ddd210e135db949a1a4a9f2a062022bd8d674aa (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
{
  config,
  lib,
  modulesPath,
  nixLib,
  ...
}: let
  cfg = config.vhack.nix-sync;

  mkNixSyncRepository = {
    domain,
    repositoryUrl,
    extraSettings,
  }: {
    name = "${domain}";
    value = {
      path = "/etc/nginx/websites/${domain}";
      uri = "${repositoryUrl}";
      inherit extraSettings;
    };
  };
  nixSyncRepositories = builtins.listToAttrs (builtins.map mkNixSyncRepository cfg.domains);

  mkVirtHost = {
    domain,
    repositoryUrl,
    extraSettings,
  }: {
    name = "${domain}";
    value =
      # FIXME(@bpeetz): We cannot use something like `lib.recursiveUpdate` because the
      # `extraSettings` are instantiated from the “real” nginx type. As such the
      # `extaSettings` would override our values here. Therefore, the direct merge. <2025-02-07>
      extraSettings
      // {
        forceSSL = true;
        enableACME = true;
        root = "/etc/nginx/websites/${domain}";
      };
  };
  virtHosts = builtins.listToAttrs (builtins.map mkVirtHost cfg.domains);
in {
  imports = [
    ./internal_module.nix
  ];

  options.vhack.nix-sync = {
    enable = lib.mkEnableOption ''
      a website git ops solution.
    '';

    domains = lib.mkOption {
      type = lib.types.listOf (lib.types.submodule {
        options = {
          domain = lib.mkOption {
            type = lib.types.str;
            example = "b-peetz.de";
            description = ''
              The fully qualified domain to use as base of this website.
            '';
          };
          repositoryUrl = lib.mkOption {
            type = lib.types.str;
            example = "b-peetz.de";
            description = ''
              The url used for the source git repository, which is deployed at this domain.
            '';
          };
          extraSettings = lib.mkOption {
            type =
              lib.types.submodule (import (modulesPath + "/services/web-servers/nginx/vhost-options.nix") {inherit config lib;});
            example = {
              locations."/.well-known/openpgpkey/".extraConfig = "default_type application/octet-stream";
            };
            default = {};
            description = ''
              Extra configuration to add to the nginx virtual host.
            '';
          };
        };
      });
    };
  };

  config = lib.mkIf cfg.enable {
    vhack.persist.directories = [
      {
        directory = "/var/lib/nix-sync";
        user = "nix-sync";
        group = "nix-sync";
        mode = "0700";
      }
    ];

    services.nix-sync = {
      enable = true;
      repositories = nixSyncRepositories;
    };

    vhack.nginx.enable = true;
    services.nginx.virtualHosts = virtHosts;

    users = {
      users.nix-sync.uid = config.vhack.constants.ids.uids.nix-sync;
      groups.nix-sync.gid = config.vhack.constants.ids.gids.nix-sync;
    };
  };
}