From 1a7f190cb9b4176ceb5bab43a6803c3fb015671a Mon Sep 17 00:00:00 2001
From: seylerius <sable@seyleri.us>
Date: Mon, 3 Aug 2020 03:38:22 -0500
Subject: [PATCH] rofi-pass: add rofi-pass plugin for password-store

---
 .github/CODEOWNERS                            |  3 ++
 modules/lib/maintainers.nix                   | 10 ++++
 modules/misc/news.nix                         |  7 +++
 modules/modules.nix                           |  1 +
 modules/programs/rofi-pass.nix                | 46 +++++++++++++++++++
 tests/default.nix                             |  1 +
 tests/modules/programs/rofi-pass/default.nix  |  4 ++
 .../programs/rofi-pass/rofi-pass-config.nix   | 35 ++++++++++++++
 .../programs/rofi-pass/rofi-pass-root.nix     | 30 ++++++++++++
 9 files changed, 137 insertions(+)
 create mode 100644 modules/programs/rofi-pass.nix
 create mode 100644 tests/modules/programs/rofi-pass/default.nix
 create mode 100644 tests/modules/programs/rofi-pass/rofi-pass-config.nix
 create mode 100644 tests/modules/programs/rofi-pass/rofi-pass-root.nix

diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index 4a66e1a78..54be54fb8 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -119,6 +119,9 @@
 
 /modules/programs/powerline-go.nix                    @DamienCassou
 
+/modules/programs/rofi-pass.nix                       @seylerius
+/tests/modules/programs/rofi-pass                     @seylerius
+
 /modules/programs/rtorrent.nix                        @marsam
 
 /modules/programs/ssh.nix                             @rycee
diff --git a/modules/lib/maintainers.nix b/modules/lib/maintainers.nix
index 715b5b0bb..de1808015 100644
--- a/modules/lib/maintainers.nix
+++ b/modules/lib/maintainers.nix
@@ -37,4 +37,14 @@
     github = "matrss";
     githubId = 9308656;
   };
+  seylerius = {
+    email = "sable@seyleri.us";
+    name = "Sable Seyler";
+    github = "seylerius";
+    githubId = 1145981;
+    keys = [{
+      logkeyid = "rsa4096/0x68BF2EAE6D91CAFF";
+      fingerprint = "F0E0 0311 126A CD72 4392  25E6 68BF 2EAE 6D91 CAFF";
+    }];
+  };
 }
diff --git a/modules/misc/news.nix b/modules/misc/news.nix
index 1cab8d89b..db2a15abd 100644
--- a/modules/misc/news.nix
+++ b/modules/misc/news.nix
@@ -1775,6 +1775,13 @@ in
           A new module is available: 'services.pbgopy'.
         '';
       }
+
+      {
+        time = "2020-12-18T22:22:25+00:00";
+        message = ''
+          A new module is available: 'programs.rofi.pass'.
+        '';
+      }
     ];
   };
 }
diff --git a/modules/modules.nix b/modules/modules.nix
index 8ecdc462a..292037d93 100644
--- a/modules/modules.nix
+++ b/modules/modules.nix
@@ -113,6 +113,7 @@ let
     (loadModule ./programs/qutebrowser.nix { })
     (loadModule ./programs/readline.nix { })
     (loadModule ./programs/rofi.nix { })
+    (loadModule ./programs/rofi-pass.nix {  })
     (loadModule ./programs/rtorrent.nix { })
     (loadModule ./programs/skim.nix { })
     (loadModule ./programs/starship.nix { })
diff --git a/modules/programs/rofi-pass.nix b/modules/programs/rofi-pass.nix
new file mode 100644
index 000000000..da75299e6
--- /dev/null
+++ b/modules/programs/rofi-pass.nix
@@ -0,0 +1,46 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let
+
+  cfg = config.programs.rofi.pass;
+
+in {
+  meta.maintainers = [ maintainers.seylerius ];
+
+  options.programs.rofi.pass = {
+    enable = mkEnableOption "rofi integration with password-store";
+
+    stores = mkOption {
+      type = types.listOf types.str;
+      default = [ ];
+      description = ''
+        Directory roots of your password-stores.
+      '';
+    };
+
+    extraConfig = mkOption {
+      type = types.lines;
+      default = "";
+      example = ''
+        URL_field='url'
+        USERNAME_field='user'
+        AUTOTYPE_field='autotype'
+      '';
+      description = ''
+        Extra configuration to be added at to the rofi-pass config file.
+        Additional examples can be found at
+        <link xlink:href="https://github.com/carnager/rofi-pass/blob/master/config.example"/>.
+      '';
+    };
+  };
+
+  config = mkIf cfg.enable {
+    home.packages = [ pkgs.rofi-pass ];
+
+    xdg.configFile."rofi-pass/config".text = optionalString (cfg.stores != [ ])
+      ("root=" + (concatStringsSep ":" cfg.stores) + "\n") + cfg.extraConfig
+      + optionalString (cfg.extraConfig != "") "\n";
+  };
+}
diff --git a/tests/default.nix b/tests/default.nix
index 1bc54ec9b..0cfa26bbc 100644
--- a/tests/default.nix
+++ b/tests/default.nix
@@ -89,6 +89,7 @@ import nmt {
     ./modules/programs/ncmpcpp-linux
     ./modules/programs/neovim   # Broken package dependency on Darwin.
     ./modules/programs/rofi
+    ./modules/programs/rofi-pass
     ./modules/programs/waybar
     ./modules/services/dropbox
     ./modules/services/emacs
diff --git a/tests/modules/programs/rofi-pass/default.nix b/tests/modules/programs/rofi-pass/default.nix
new file mode 100644
index 000000000..181aef4e1
--- /dev/null
+++ b/tests/modules/programs/rofi-pass/default.nix
@@ -0,0 +1,4 @@
+{
+  rofi-pass-root = ./rofi-pass-root.nix;
+  rofi-pass-config = ./rofi-pass-config.nix;
+}
diff --git a/tests/modules/programs/rofi-pass/rofi-pass-config.nix b/tests/modules/programs/rofi-pass/rofi-pass-config.nix
new file mode 100644
index 000000000..e70ad9ab1
--- /dev/null
+++ b/tests/modules/programs/rofi-pass/rofi-pass-config.nix
@@ -0,0 +1,35 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+{
+  config = {
+    programs.rofi = {
+      enable = true;
+
+      pass = {
+        enable = true;
+        extraConfig = ''
+          # Extra config for rofi-pass
+          xdotool_delay=12
+        '';
+      };
+    };
+
+    nixpkgs.overlays = [
+      (self: super: { rofi-pass = pkgs.writeScriptBin "dummy-rofi-pass" ""; })
+    ];
+
+    nmt.script = ''
+      assertFileContent \
+        home-files/.config/rofi-pass/config \
+        ${
+          pkgs.writeText "rofi-pass-expected-config" ''
+            # Extra config for rofi-pass
+            xdotool_delay=12
+
+          ''
+        }
+    '';
+  };
+}
diff --git a/tests/modules/programs/rofi-pass/rofi-pass-root.nix b/tests/modules/programs/rofi-pass/rofi-pass-root.nix
new file mode 100644
index 000000000..15c86ef62
--- /dev/null
+++ b/tests/modules/programs/rofi-pass/rofi-pass-root.nix
@@ -0,0 +1,30 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+{
+  config = {
+    programs.rofi = {
+      enable = true;
+
+      pass = {
+        enable = true;
+        stores = [ "~/.local/share/password-store" ];
+      };
+    };
+
+    nixpkgs.overlays = [
+      (self: super: { rofi-pass = pkgs.writeScriptBin "dummy-rofi-pass" ""; })
+    ];
+
+    nmt.script = ''
+      assertFileContent \
+        home-files/.config/rofi-pass/config \
+        ${
+          pkgs.writeText "rofi-pass-expected-config" ''
+            root=~/.local/share/password-store
+          ''
+        }
+    '';
+  };
+}