From fea7e110c26a007ea64874277fdd31aa52557bd2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= <joerg@thalheim.io>
Date: Sat, 31 Dec 2022 07:18:49 +0100
Subject: [PATCH] add treefmt-nix to repository

---
 ci.nix      |  2 +-
 flake.lock  | 18 ++++++++++++++++-
 flake.nix   |  8 ++++++++
 shell.nix   |  2 ++
 treefmt.nix | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 86 insertions(+), 2 deletions(-)
 create mode 100644 treefmt.nix

diff --git a/ci.nix b/ci.nix
index 4ae91f4..8d31950 100644
--- a/ci.nix
+++ b/ci.nix
@@ -12,4 +12,4 @@ in
   # FIXME: maybe find a more generic solution here?
   devShell-x86_64 = self.outputs.devShells.x86_64-linux.default;
   devShell-aarch64 = self.outputs.devShells.aarch64-linux.default;
-}
+} // self.outputs.checks.x86_64-linux # mainly for treefmt at the moment...
diff --git a/flake.lock b/flake.lock
index 945ceb4..fafc3b7 100644
--- a/flake.lock
+++ b/flake.lock
@@ -223,7 +223,8 @@
         "nixpkgs-update-pypi-releases": "nixpkgs-update-pypi-releases",
         "nur-update": "nur-update",
         "sops-nix": "sops-nix",
-        "srvos": "srvos"
+        "srvos": "srvos",
+        "treefmt-nix": "treefmt-nix"
       }
     },
     "sops-nix": {
@@ -266,6 +267,21 @@
         "repo": "srvos",
         "type": "github"
       }
+    },
+    "treefmt-nix": {
+      "locked": {
+        "lastModified": 1672170030,
+        "narHash": "sha256-hvTMwlutePPQ4eNMVHiI0crixCyeSTKJIDhLD/66t2g=",
+        "owner": "numtide",
+        "repo": "treefmt-nix",
+        "rev": "c97bb3db343ccd2f04dd4e5fa8750e821560e9ca",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "treefmt-nix",
+        "type": "github"
+      }
     }
   },
   "root": "root",
diff --git a/flake.nix b/flake.nix
index e58a18c..0510848 100644
--- a/flake.nix
+++ b/flake.nix
@@ -31,6 +31,8 @@
 
     disko.url = "github:nix-community/disko";
     disko.inputs.nixpkgs.follows = "nixpkgs";
+
+    treefmt-nix.url = "github:numtide/treefmt-nix";
   };
 
   outputs = inputs @ {flake-parts, ...}:
@@ -39,13 +41,19 @@
       {
         systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
 
+        imports = [
+          ./treefmt.nix
+        ];
+
         perSystem = {
           inputs',
           pkgs,
+          self',
           ...
         }: {
           devShells.default = pkgs.callPackage ./shell.nix {
             inherit (inputs'.sops-nix.packages) sops-import-keys-hook;
+            inherit (self'.packages) treefmt;
           };
         };
         flake.nixosConfigurations = let
diff --git a/shell.nix b/shell.nix
index 7ac39f7..09b340e 100644
--- a/shell.nix
+++ b/shell.nix
@@ -1,5 +1,6 @@
 { pkgs
 , sops-import-keys-hook
+, treefmt
 }:
 
 with pkgs;
@@ -26,6 +27,7 @@ mkShellNoCC {
       ]
     ))
     rsync
+    treefmt
 
     sops-import-keys-hook
   ];
diff --git a/treefmt.nix b/treefmt.nix
new file mode 100644
index 0000000..6ec5391
--- /dev/null
+++ b/treefmt.nix
@@ -0,0 +1,58 @@
+{ inputs, ... }: {
+  imports = [
+    inputs.treefmt-nix.flakeModule
+  ];
+  perSystem = { pkgs, ... }: {
+    treefmt = {
+      # Used to find the project root
+      projectRootFile = "flake.lock";
+
+      programs.terraform.enable = true;
+
+      settings.formatter = {
+        nix = {
+          command = "sh";
+          options = [
+            "-eucx"
+            ''
+              # First deadnix
+              ${pkgs.lib.getExe pkgs.deadnix} --edit "$@"
+              # Then nixpkgs-fmt
+              ${pkgs.lib.getExe pkgs.nixpkgs-fmt} "$@"
+            ''
+            "--"
+          ];
+          includes = [ "*.nix" ];
+          excludes = [ "nix/sources.nix" ];
+        };
+        shell = {
+          command = "sh";
+          options = [
+            "-eucx"
+            ''
+              # First shellcheck
+              ${pkgs.lib.getExe pkgs.shellcheck} --external-sources --source-path=SCRIPTDIR "$@"
+              # Then format
+              ${pkgs.lib.getExe pkgs.shfmt} -i 2 -s -w "$@"
+            ''
+            "--"
+          ];
+          includes = [ "*.sh" ];
+        };
+
+        python = {
+          command = "sh";
+          options = [
+            "-eucx"
+            ''
+              ${pkgs.lib.getExe pkgs.ruff} --fix "$@"
+              ${pkgs.lib.getExe pkgs.python3.pkgs.black} "$@"
+            ''
+            "--" # this argument is ignored by bash
+          ];
+          includes = [ "*.py" ];
+        };
+      };
+    };
+  };
+}