From 8a10ae84c0cfa6ecc0fce6f20fca7e29c414f064 Mon Sep 17 00:00:00 2001
From: Akshay Mankar <itsakshaymankar@gmail.com>
Date: Fri, 29 Apr 2022 16:03:49 +0200
Subject: [PATCH] Use nix-flakes

---
 .envrc        |  3 +--
 .gitignore    |  3 ++-
 README.md     |  3 ++-
 cabal.project |  8 +-------
 default.nix   | 32 ++++++++++++++++++-------------
 direnv.nix    | 18 -----------------
 flake.lock    | 43 +++++++++++++++++++++++++++++++++++++++++
 flake.nix     | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 121 insertions(+), 42 deletions(-)
 delete mode 100644 direnv.nix
 create mode 100644 flake.lock
 create mode 100644 flake.nix

diff --git a/.envrc b/.envrc
index 5a9b66b..44610e5 100644
--- a/.envrc
+++ b/.envrc
@@ -1,2 +1 @@
-env="$(nix-build $PWD/direnv.nix -A env)"
-load_prefix "${env}"
+use flake;
diff --git a/.gitignore b/.gitignore
index da66d29..17bcffc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 result
 dist-newstyle
-.dir-locals.el
\ No newline at end of file
+.dir-locals.el
+.direnv
\ No newline at end of file
diff --git a/README.md b/README.md
index 97ef7a1..1ec7220 100644
--- a/README.md
+++ b/README.md
@@ -33,7 +33,8 @@ used.
      ```
    - Using [nix](https://nixos.org/):
      ```bash
-     nix-env -f https://git.coop/akshay/terraform-http-backend-pass/-/archive/main/terraform-http-backend-pass-main.tar.gz -i
+     nix registry add terraform-http-backend-pass git+https://git.coop/akshay/terraform-http-backend-pass
+     nix build 'terraform-http-backend-pass#' --profile $HOME/.nix-profile
      ```
 2. Create a pass repository:
    ```bash
diff --git a/cabal.project b/cabal.project
index b512e20..5356e76 100644
--- a/cabal.project
+++ b/cabal.project
@@ -1,7 +1 @@
-packages: .
-
-source-repository-package
-  type: git
-  location: https://github.com/haskell-servant/servant.git
-  tag: 27173c922311112dd153346cf3cd72b9fb0f3551
-  subdir: servant servant-server
\ No newline at end of file
+packages: .
\ No newline at end of file
diff --git a/default.nix b/default.nix
index 5b6d87c..926ce12 100644
--- a/default.nix
+++ b/default.nix
@@ -1,13 +1,19 @@
-let sources = import ./nix/sources.nix;
-in { pkgs ? import sources.nixpkgs {},
-     compiler ? "ghc8103"
-   }:
-     let
-       servantPkgs = import sources.servant {inherit pkgs compiler; };
-       overrides = self: super: {
-         terraform-http-backend-pass = self.callCabal2nix "terraform-http-backend-pass" ./. {};
-         servant = servantPkgs.servant;
-         servant-server = servantPkgs.servant-server;
-       };
-       hPkgs = pkgs.haskell.packages.${compiler}.override { inherit overrides; };
-     in hPkgs.terraform-http-backend-pass
+{ mkDerivation, aeson, base, bytestring, directory, lib, mtl
+, optparse-applicative, optparse-generic, servant, servant-server
+, shelly, text, warp
+}:
+mkDerivation {
+  pname = "terraform-http-backend-pass";
+  version = "0.1.0.0";
+  src = ./.;
+  isLibrary = true;
+  isExecutable = true;
+  libraryHaskellDepends = [
+    aeson base bytestring directory mtl optparse-applicative
+    optparse-generic servant servant-server shelly text warp
+  ];
+  executableHaskellDepends = [ base ];
+  homepage = "https://github.com/akshaymankar/terraform-http-backend-pass#readme";
+  description = "HTTP backend to store terraform state using pass and git";
+  license = "AGPL";
+}
diff --git a/direnv.nix b/direnv.nix
deleted file mode 100644
index b596732..0000000
--- a/direnv.nix
+++ /dev/null
@@ -1,18 +0,0 @@
-let
-  sources = import ./nix/sources.nix;
-  pkgs = import sources.nixpkgs {};
-in {
-  env = pkgs.buildEnv {
-    name = "terraform-http-backend-pass";
-    paths = with pkgs; [
-      pass
-      niv
-      gnumake
-      haskell-language-server
-      cabal-install
-      haskell.compiler.ghc8103 # HLS doesn't support GHC 9 yet.
-      zlib.dev
-      zlib
-    ];
-  };
-}
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..6bdcae2
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,43 @@
+{
+  "nodes": {
+    "flake-utils": {
+      "locked": {
+        "lastModified": 1649676176,
+        "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1651114127,
+        "narHash": "sha256-/lLC0wkMZkAdA5e1W76SnJzbhfOGDvync3VRHJMtAKk=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "6766fb6503ae1ebebc2a9704c162b2aef351f921",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "nixpkgs-unstable",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "flake-utils": "flake-utils",
+        "nixpkgs": "nixpkgs"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..3e3ac29
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,53 @@
+{
+  description = "A very basic flake";
+
+  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
+  inputs.flake-utils.url = "github:numtide/flake-utils";
+
+  outputs = {nixpkgs, flake-utils, ...}:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs {inherit system;};
+        hlib = pkgs.haskell.lib;
+        # Avoids unnecessary recompiles
+        filteredSource = pkgs.lib.cleanSourceWith {
+          src = ./.;
+          filter = path: type:
+            let baseName = baseNameOf (toString path);
+            in pkgs.lib.cleanSourceFilter path type && !(
+              baseName == "flake.nix" ||
+              baseName == "flake.lock" ||
+              baseName == "dist-newstyle" ||
+              baseName == "nix" ||
+              builtins.match "^cabal\.project\..*$" baseName != null ||
+              baseName == ".envrc" ||
+              baseName == "hie.yaml" ||
+              baseName == ".hlint.yaml" ||
+              baseName == ".hspec"
+            );
+        };
+        haskellPackages = pkgs.haskell.packages.ghc8107.override {
+          overrides = hself: hsuper:
+            {
+              terraform-http-backend-pass = hlib.overrideSrc (hself.callPackage ./default.nix {}) {src = filteredSource;};
+            };
+        };
+      in rec {
+        packages = {
+          terraform-http-backend-pass = haskellPackages.terraform-http-backend-pass;
+          dev-shell = haskellPackages.shellFor {
+            packages = p: [p.terraform-http-backend-pass];
+            buildInputs = [
+              pkgs.cabal-install
+              pkgs.haskell.packages.ghc8107.haskell-language-server
+              pkgs.cabal2nix
+
+              pkgs.pass
+            ];
+          };
+        };
+        defaultPackage = packages.terraform-http-backend-pass;
+        devShell = packages.dev-shell;
+      });
+}
+
-- 
GitLab