summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/apt-cacher-ng.nix
blob: c2c2f2661c00c3dd43b53f74c70b2ad2a5ae7916 (plain)
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
{ config, pkgs, lib, ... }:

with lib;
let
  acng-config = pkgs.writeTextFile {
    name = "acng-configuration";
    destination = "/acng.conf";
    text = ''
    ForeGround: 1
    CacheDir: ${cfg.cacheDir}
    LogDir: ${cfg.logDir}
    PidFile: /var/run/apt-cacher-ng.pid
    ExTreshold: ${toString cfg.cacheExpiration}

    Port: ${toString cfg.port}
    BindAddress: ${cfg.bindAddress}

    # defaults:
    Remap-debrep: file:deb_mirror*.gz /debian ; file:backends_debian
    Remap-uburep: file:ubuntu_mirrors /ubuntu ; file:backends_ubuntu
    Remap-debvol: file:debvol_mirror*.gz /debian-volatile ; file:backends_debvol
    Remap-cygwin: file:cygwin_mirrors /cygwin
    Remap-sfnet:  file:sfnet_mirrors
    Remap-alxrep: file:archlx_mirrors /archlinux
    Remap-fedora: file:fedora_mirrors
    Remap-epel:   file:epel_mirrors
    Remap-slrep:  file:sl_mirrors # Scientific Linux
    Remap-gentoo: file:gentoo_mirrors.gz /gentoo ; file:backends_gentoo

    ReportPage: acng-report.html
    SupportDir: ${pkgs.apt-cacher-ng}/lib/apt-cacher-ng
    LocalDirs: acng-doc ${pkgs.apt-cacher-ng}/share/doc/apt-cacher-ng

    # Nix cache
    ${optionalString cfg.enableNixCache ''
      Remap-nix: http://cache.nixos.org /nixos ; https://cache.nixos.org
      PfilePatternEx: (^|.*?/).*\.narinfo(|\.gz|\.xz|\.bz2)$
      VfilePatternEx: (^|.*?/)nix-cache-info$
    ''}

    ${cfg.extraConfig}
  ''; };

  acng-home = "/var/cache/acng";
  cfg = config.krebs.apt-cacher-ng;

  api = {
    enable = mkEnableOption "apt-cacher-ng";

    cacheDir = mkOption {
      default = acng-home + "/cache";
      type = types.str;
      description = ''
        Path to apt-cacher-ng cache directory.
        Will be created and chowned to acng-user
      '';
    };

    logDir = mkOption {
      default = acng-home + "/log";
      type = types.str;
      description = ''
        Path to apt-cacher-ng log directory.
        Will be created and chowned to acng-user
      '';
    };

    port = mkOption {
      default = 3142;
      type = types.int;
      description = ''
        port of apt-cacher-ng
      '';
    };

    bindAddress = mkOption {
      default = "";
      type = types.str;
      example = "localhost 192.168.7.254 publicNameOnMainInterface";
      description = ''
        listen address of apt-cacher-ng. Defaults to every interface.
      '';
    };

    cacheExpiration = mkOption {
      default = 4;
      type = types.int;
      description = ''
        number of days before packages expire in the cache without being
        requested.
      '';
    };

    enableNixCache = mkOption {
      default = true;
      type = types.bool;
      description = ''
        enable cache.nixos.org caching via PfilePatternEx and VfilePatternEx.

        to use the apt-cacher-ng in your nixos configuration:
          nix.binary-cache = [ http://acng-host:port/nixos ];

        These options cannot be used in extraConfig, use SVfilePattern and
        SPfilePattern or disable this option.
      '';
    };

    extraConfig = mkOption {
      default = "";
      type = types.lines;
      description = ''
        extra config appended to the generated acng.conf
      '';
    };
  };

  imp = {

    users.extraUsers.acng = {
    # uid = config.ids.uids.acng;
      uid = 897955083; #genid Reaktor
      description = "apt-cacher-ng";
      home = acng-home;
      createHome = false;
    };

    users.extraGroups.acng = {
      gid = 897955083; #genid Reaktor
    # gid = config.ids.gids.Reaktor;
    };

    systemd.services.apt-cacher-ng = {
      description = "apt-cacher-ng";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        PermissionsStartOnly = true;
        ExecStartPre = pkgs.writeScript "acng-init" ''
          #!/bin/sh
          mkdir -p ${shell.escape cfg.cacheDir} ${shell.escape cfg.logDir}
          chown acng:acng  ${shell.escape cfg.cacheDir} ${shell.escape cfg.logDir}
        '';
        ExecStart = "${pkgs.apt-cacher-ng}/bin/apt-cacher-ng -c ${acng-config}";
        PrivateTmp = "true";
        User = "acng";
        Restart = "always";
        RestartSec = "10";
      };
    };
  };
in
{
  options.krebs.apt-cacher-ng = api;
  config = mkIf cfg.enable imp;
}