From 7846843beae1d5638be516381c0bcfc9c757301f Mon Sep 17 00:00:00 2001
From: Pablo Ovelleiro Corral <mail@pablo.tools>
Date: Thu, 28 Mar 2024 11:04:10 +0100
Subject: [PATCH] Add flake

---
 .gitignore |  1 +
 README.md  |  5 +++++
 flake.lock | 26 ++++++++++++++++++++++++++
 flake.nix  | 45 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+)
 create mode 100644 flake.lock
 create mode 100644 flake.nix

diff --git a/.gitignore b/.gitignore
index eebe93d..04c5c6e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 pgp2ssh
+result
diff --git a/README.md b/README.md
index 7401239..f79632b 100644
--- a/README.md
+++ b/README.md
@@ -34,6 +34,11 @@ If you have multiple subkeys, usually it is the authenticate key highlighted wit
 ❯ ./pgp2ssh
 ```
 
+**Nix/NixOS Users**
+
+A flake is provided for Nix users. Just use `nix run` instead of building and
+running manually.
+
 It'll ask you for the path to your private PGP key, followed by choosing the key/subkey and if your PGP key is encrypted it'll ask for the passphrase.
 
 In the output, verify that the public SSH key printed matches the one in `metadata.json`.
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..c067efc
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,26 @@
+{
+  "nodes": {
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1711523803,
+        "narHash": "sha256-UKcYiHWHQynzj6CN/vTcix4yd1eCu1uFdsuarupdCQQ=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "2726f127c15a4cc9810843b96cad73c7eb39e443",
+        "type": "github"
+      },
+      "original": {
+        "id": "nixpkgs",
+        "ref": "nixos-unstable",
+        "type": "indirect"
+      }
+    },
+    "root": {
+      "inputs": {
+        "nixpkgs": "nixpkgs"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..9087e58
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,45 @@
+{
+  description = "Convert GPG/PGP Keys to SSH private keys";
+
+  # Nixpkgs / NixOS version to use.
+  inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
+
+  outputs = { self, nixpkgs }:
+    let
+
+      # to work with older version of flakes
+      lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";
+
+      # Generate a user-friendly version number.
+      version = builtins.substring 0 8 lastModifiedDate;
+
+      # System types to support.
+      supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
+
+      # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
+      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
+
+      # Nixpkgs instantiated for supported system types.
+      nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
+
+    in
+    {
+
+      # Provide some binary packages for selected system types.
+      packages = forAllSystems (system:
+        let
+          pkgs = nixpkgsFor.${system};
+        in
+        {
+          pgp2ssh = pkgs.buildGoModule {
+            pname = "pgp2ssh";
+            inherit version;
+            src = ./.;
+            vendorHash = "sha256-O4AeSfdJxSGnWwRkNnAQMnOZE+Auy+3BIjncG/PK5EE=";
+          };
+        });
+
+      # The default package for 'nix build' and 'nix run'
+      defaultPackage = forAllSystems (system: self.packages.${system}.pgp2ssh);
+    };
+}