Browse Source

ci(nix): add treefmt, rust-toolchain, nixfmt

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
Christina Sørensen 2 years ago
parent
commit
05df8c9fea
4 changed files with 201 additions and 24 deletions
  1. 112 9
      flake.lock
  2. 78 14
      flake.nix
  3. 3 1
      rust-toolchain.toml
  4. 8 0
      treefmt.nix

+ 112 - 9
flake.lock

@@ -1,5 +1,41 @@
 {
   "nodes": {
+    "flake-utils": {
+      "inputs": {
+        "systems": "systems"
+      },
+      "locked": {
+        "lastModified": 1689068808,
+        "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
+    "flake-utils_2": {
+      "inputs": {
+        "systems": "systems_2"
+      },
+      "locked": {
+        "lastModified": 1681202837,
+        "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
     "naersk": {
       "inputs": {
         "nixpkgs": "nixpkgs"
@@ -14,7 +50,6 @@
       },
       "original": {
         "owner": "nix-community",
-        "ref": "master",
         "repo": "naersk",
         "type": "github"
       }
@@ -48,11 +83,64 @@
         "type": "github"
       }
     },
+    "nixpkgs_3": {
+      "locked": {
+        "lastModified": 1681358109,
+        "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "nixpkgs-unstable",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "nixpkgs_4": {
+      "locked": {
+        "lastModified": 1680945546,
+        "narHash": "sha256-8FuaH5t/aVi/pR1XxnF0qi4WwMYC+YxlfdsA0V+TEuQ=",
+        "owner": "nixos",
+        "repo": "nixpkgs",
+        "rev": "d9f759f2ea8d265d974a6e1259bd510ac5844c5d",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nixos",
+        "ref": "nixos-unstable",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
     "root": {
       "inputs": {
+        "flake-utils": "flake-utils",
         "naersk": "naersk",
         "nixpkgs": "nixpkgs_2",
-        "utils": "utils"
+        "rust-overlay": "rust-overlay",
+        "treefmt-nix": "treefmt-nix"
+      }
+    },
+    "rust-overlay": {
+      "inputs": {
+        "flake-utils": "flake-utils_2",
+        "nixpkgs": "nixpkgs_3"
+      },
+      "locked": {
+        "lastModified": 1690596958,
+        "narHash": "sha256-SWqxUiEP9O2gvlWtR4Ku6rIMGM7PuNZreAPrU2yAjsk=",
+        "owner": "oxalica",
+        "repo": "rust-overlay",
+        "rev": "5c06b0ed7bfb00f3a925af6c4acd1636596381c1",
+        "type": "github"
+      },
+      "original": {
+        "owner": "oxalica",
+        "repo": "rust-overlay",
+        "type": "github"
       }
     },
     "systems": {
@@ -70,21 +158,36 @@
         "type": "github"
       }
     },
-    "utils": {
+    "systems_2": {
+      "locked": {
+        "lastModified": 1681028828,
+        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
+        "owner": "nix-systems",
+        "repo": "default",
+        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-systems",
+        "repo": "default",
+        "type": "github"
+      }
+    },
+    "treefmt-nix": {
       "inputs": {
-        "systems": "systems"
+        "nixpkgs": "nixpkgs_4"
       },
       "locked": {
-        "lastModified": 1689068808,
-        "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
+        "lastModified": 1689620039,
+        "narHash": "sha256-BtNwghr05z7k5YMdq+6nbue+nEalvDepuA7qdQMAKoQ=",
         "owner": "numtide",
-        "repo": "flake-utils",
-        "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
+        "repo": "treefmt-nix",
+        "rev": "719c2977f958c41fa60a928e2fbc50af14844114",
         "type": "github"
       },
       "original": {
         "owner": "numtide",
-        "repo": "flake-utils",
+        "repo": "treefmt-nix",
         "type": "github"
       }
     }

+ 78 - 14
flake.nix

@@ -1,21 +1,85 @@
 {
+
   inputs = {
-    naersk.url = "github:nix-community/naersk/master";
+
+    flake-utils.url = "github:numtide/flake-utils";
+
+    naersk.url = "github:nix-community/naersk";
+
     nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
-    utils.url = "github:numtide/flake-utils";
+
+    treefmt-nix.url = "github:numtide/treefmt-nix";
+
+    rust-overlay.url = "github:oxalica/rust-overlay";
+
   };
 
-  outputs = { self, nixpkgs, utils, naersk }:
-    utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-        naersk-lib = pkgs.callPackage naersk { };
-      in
-      {
-        defaultPackage = naersk-lib.buildPackage ./.;
-        devShell = with pkgs; mkShell {
-          buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy ];
-          RUST_SRC_PATH = rustPlatform.rustLibSrc;
+  outputs = {
+      self
+    , flake-utils
+    , naersk
+    , nixpkgs
+    , treefmt-nix
+    , rust-overlay
+  }:
+    flake-utils.lib.eachDefaultSystem (
+
+      system: let
+
+        overlays = [(import rust-overlay)];
+
+        pkgs = (import nixpkgs) {
+
+          inherit system overlays;
+
+        };
+
+        toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
+
+        naersk' = pkgs.callPackage naersk {
+
+          cargo = toolchain;
+
+          rustc = toolchain;
+
+        };
+
+        treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
+
+      in {
+
+        # For `nix fmt`
+
+        formatter = treefmtEval.config.build.wrapper;
+
+        # For `nix build` & `nix run`:
+
+        packages.default = naersk'.buildPackage {
+
+          src = ./.;
+
+        };
+
+        # For `nix develop`:
+
+        devShells.default = pkgs.mkShell {
+
+          nativeBuildInputs = with pkgs; [toolchain];
+
         };
-      });
+
+        # for `nix flake check`
+
+        checks = {
+
+          formatting = treefmtEval.config.build.check self;
+
+        };
+
+      }
+
+    );
+
 }
+
+

+ 3 - 1
rust-toolchain.toml

@@ -1,2 +1,4 @@
 [toolchain]
-channel = "1.63.0"
+channel = "nightly"
+components = [ "rustfmt", "rustc", "rust-src", "rust-analyzer", "cargo" ]
+profile = "minimal"

+ 8 - 0
treefmt.nix

@@ -0,0 +1,8 @@
+{
+  projectRootFile = "Cargo.toml";
+  programs = {
+    # alejandra.enable = true;
+    rustfmt.enable = true;
+  };
+}
+