Commit 5d7f6440 authored by Gaute Ravndal's avatar Gaute Ravndal
Browse files

nixos/man-db: add option to generate cache at runtime

Enables faster and fully asynchronous generation of the cache at runtime
by maintaining a separate copy of `cfg.manualPages` with appropriate
timestamps so that mandb can pick up relevant changes without having to
re-generate the full cache on nearly every system rebuild.
parent 3fff19ce
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -277,6 +277,16 @@ in
        '';
      };

      man.cache.generateAtRuntime = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to generate the manual page index caches at runtime using
          a systemd service. Note that this is currently only supported by the
          man-db module.
        '';
      };

      info.enable = mkOption {
        type = types.bool;
        default = true;
+74 −35
Original line number Diff line number Diff line
@@ -7,13 +7,14 @@

let
  cfg = config.documentation.man.man-db;
  cfgm = config.documentation.man;
in

{
  options = {
    documentation.man.man-db = {
      enable = lib.mkEnableOption "man-db as the default man page viewer" // {
        default = config.documentation.man.enable;
        default = cfgm.enable;
        defaultText = lib.literalExpression "config.documentation.man.enable";
        example = false;
      };
@@ -65,7 +66,9 @@ in
    )
  ];

  config = lib.mkIf cfg.enable {
  config = lib.mkIf cfg.enable (
    lib.mkMerge [
      {
        environment.systemPackages = [ cfg.package ];
        environment.etc."man_db.conf".text =
          let
@@ -79,6 +82,7 @@ in
                pkgs.buildPackages.man-db;

            manualCache =
              if (!cfgm.cache.generateAtRuntime) then
                pkgs.runCommand "man-cache"
                  {
                    nativeBuildInputs = [ buildPackage ];
@@ -87,19 +91,54 @@ in
                  ''
                    echo "MANDB_MAP ${cfg.manualPages}/share/man $out" > man.conf
                    mandb -C man.conf -pscq
            '';
                  ''
              else
                "/var/cache/man/nixos-mandb";
          in
          ''
            # Manual pages paths for NixOS
            MANPATH_MAP /run/current-system/sw/bin /run/current-system/sw/share/man
            MANPATH_MAP /run/wrappers/bin          /run/current-system/sw/share/man

        ${lib.optionalString config.documentation.man.cache.enable ''
          # Generated manual pages cache for NixOS (immutable)
            ${lib.optionalString cfgm.cache.enable ''
              # Manual pages caches for NixOS
              MANDB_MAP /run/current-system/sw/share/man ${manualCache}
            ''}
        # Manual pages caches for NixOS
        MANDB_MAP /run/current-system/sw/share/man /var/cache/man/nixos
          '';
      }

      (lib.mkIf (cfgm.enable && cfgm.cache.generateAtRuntime) {
        users.users.mandb = {
          isSystemUser = true;
          group = "mandb";
        };
        users.groups.mandb = { };

        systemd.services.mandb = {
          path = [
            cfg.package
            pkgs.rsync
          ];
          script = ''
            rsync \
              --checksum --recursive --copy-links --delete --no-times --no-perms --chmod=+w \
              ${cfg.manualPages}/share/man/ "$CACHE_DIRECTORY/nixos-manpages"

            echo "MANDB_MAP $CACHE_DIRECTORY/nixos-manpages $CACHE_DIRECTORY/nixos-mandb" \
              > "$RUNTIME_DIRECTORY/man.conf"

            mandb -C "$RUNTIME_DIRECTORY/man.conf" -q
          '';
          serviceConfig = {
            CacheDirectory = "man";
            RuntimeDirectory = "mandb";
            User = "mandb";
            BindReadOnlyPaths = [ "/dev/null:/etc/man_db.conf" ]; # mandb will still read /etc/man_db.conf if it exists, even when setting -C path/to/config.conf
            ProtectSystem = "strict";
          };
          wantedBy = [ "default.target" ];
        };
      })
    ]
  );
}