aboutsummaryrefslogtreecommitdiff
path: root/modules/pass/default.nix
blob: a92c331bc006ff6c5f4aee733c9f59d183bd42aa (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
{ config, lib, pkgs, ... }:
let
  cfg = config.modules.pass;

  inherit (lib) mkEnableOption mkOption mkIf types;
in
{
  options.modules.pass = {
    enable = mkEnableOption "pass";
    enableExtensions = mkEnableOption "extensions";
    key = mkOption {
      type = types.nullOr types.str;
      description = "gpg key, spaces forbidden";
      default = "";
    };
    wmenu = mkOption {
      type = types.str;
      description = "dmenu command to use for passmenu in a wayland session";
      default = "wmenu -n 4c4c4c -N 0d0d0d -s 8aac8b -S 0d0d0d -l 10";
    };
    xmenu = mkOption {
      type = types.str;
      description = "dmenu command to use for passmenu in an x session";
      default = "dmenu";
    };
  };

  config = mkIf cfg.enable {
    home.packages = with pkgs; [(writeShellScriptBin "passmenu" ''
      shopt -s nullglob globstar

      if [[ -n $WAYLAND_DISPLAY ]]; then
        menu="${cfg.wmenu}"
      elif [[ -n $DISPLAY ]]; then
        menu="${cfg.xmenu}" # fallback to dmenu if running under x11
      else
        echo "Error: No Wayland or X11 display detected" >&2
        exit 1
      fi

      prefix=''${PASSWORD_STORE_DIR-~/.password-store}
      password_files=( "$prefix"/**/*.gpg )
      password_files=( "''${password_files[@]#"$prefix"/}" )
      password_files=( "''${password_files[@]%.gpg}" )

      password=$(printf '%s\n' "''${password_files[@]}" | $menu "$@")

      [[ -n $password ]] || exit

      pass show -c "$password" 2>/dev/null
    '')];
    programs.password-store = {
      package = pkgs.pass.override {
        dmenuSupport = false;
      };
      enable = true;
      settings = {
        PASSWORD_STORE_DIR = "${config.home.homeDirectory}/.password-store";
        PASSWORD_STORE_CHARACTER_SET=''[:alnum:]*!@^&_\-=[]|;~,./?'';
        PASSWORD_STORE_CLIP_TIME="25";
        PASSWORD_STORE_KEY=cfg.key;
        PASSWORD_STORE_ENABLE_EXTENSIONS = if cfg.enableExtensions then "1" else "0";
      };
    };
  };
}