From 985c6a5f353de306e63e49374d5bfa579b1df465 Mon Sep 17 00:00:00 2001
From: 0x4A6F <0x4A6F@users.noreply.github.com>
Date: Mon, 28 Dec 2020 14:52:17 +0000
Subject: [PATCH] nixify & flakeify & direnvify

---
 .envrc     |  7 +++++++
 .gitignore |  2 ++
 README.md  | 12 ++++++++++++
 flake.lock | 43 +++++++++++++++++++++++++++++++++++++++++++
 flake.nix  | 19 +++++++++++++++++++
 shell.nix  | 22 ++++++++++++++++++++++
 6 files changed, 105 insertions(+)
 create mode 100644 .envrc
 create mode 100644 .gitignore
 create mode 100644 flake.lock
 create mode 100644 flake.nix
 create mode 100644 shell.nix

diff --git a/.envrc b/.envrc
new file mode 100644
index 0000000..3f7ee83
--- /dev/null
+++ b/.envrc
@@ -0,0 +1,7 @@
+use flake
+
+use_flake() {
+  watch_file flake.nix
+  watch_file flake.lock
+  eval "$(nix print-dev-env --profile "$(direnv_layout_dir)/flake-profile")"
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..20cdc74
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.direnv/
+.history
diff --git a/README.md b/README.md
index f67f1b1..d39524e 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,18 @@ pip:
 1. Enter virtualenv: `source my-venv-directory/bin/activate`
 1. Install ansible: `pip install ansible`
 
+#### Using nix
+
+Use `nix-shell` or `nix develop` to use ansible.
+
+Update to newer nixpkgs version (check functionality and add a commit afterwards):
+
+`nix flake update --recreate-lock-file --update-input nixpkgs`
+
+#### Using direnv
+
+Run `mkdir .direnv && direnv allow .` to enable direnv integration with nix flakes.
+
 ### SSH Aliases
 
 Some hosts are only reachable through a jumphost, e.g. Containers or VMs on a
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..2cc10a0
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,43 @@
+{
+  "nodes": {
+    "flake-utils": {
+      "locked": {
+        "lastModified": 1609246779,
+        "narHash": "sha256-eq6ZXE/VWo3EMC65jmIT6H/rrUc9UWOWVujkzav025k=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "08c7ad4a0844adc4a7f9f5bb3beae482e789afa4",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1609187327,
+        "narHash": "sha256-6lCwWpGpZxbSYclryv4ixsxxbISe7HuLyyQnGWCC8ZQ=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "e065200fc90175a8f6e50e76ef10a48786126e1c",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "nixos-20.09",
+        "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..0565344
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,19 @@
+{
+  description = "Freifunk Stuttgart - ansible";
+
+  # To update all inputs:
+  # $ nix flake update --recreate-lock-file
+  inputs = {
+    flake-utils.url = "github:numtide/flake-utils";
+    nixpkgs.url = "github:NixOS/nixpkgs/nixos-20.09";
+  };
+
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem
+      (system:
+        let pkgs = nixpkgs.legacyPackages.${system}; in
+        {
+          devShell = import ./shell.nix { inherit pkgs; };
+        }
+      );
+}
diff --git a/shell.nix b/shell.nix
new file mode 100644
index 0000000..6ab9c17
--- /dev/null
+++ b/shell.nix
@@ -0,0 +1,22 @@
+let
+  lock = builtins.fromJSON (builtins.readFile ./flake.lock);
+in
+{
+ pkgs ? import (fetchTarball {
+   url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz";
+   sha256 = lock.nodes.nixpkgs.locked.narHash;
+ }) { }
+}:
+
+with pkgs;
+
+mkShell {
+  name = "ansible-shell";
+  buildInputs = [
+    ansible_2_10
+    ansible-lint
+    git-crypt
+    sshpass
+  ];
+  HISTFILE = "${toString ./.}/.history";
+}
-- 
GitLab