about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--flake.lock43
-rw-r--r--flake.nix36
-rw-r--r--kittybox.nix62
3 files changed, 84 insertions, 57 deletions
diff --git a/flake.lock b/flake.lock
index f0cb16d..11be16b 100644
--- a/flake.lock
+++ b/flake.lock
@@ -1,5 +1,25 @@
 {
   "nodes": {
+    "crane": {
+      "inputs": {
+        "nixpkgs": [
+          "nixpkgs"
+        ]
+      },
+      "locked": {
+        "lastModified": 1721842668,
+        "narHash": "sha256-k3oiD2z2AAwBFLa4+xfU+7G5fisRXfkvrMTCJrjZzXo=",
+        "owner": "ipetkov",
+        "repo": "crane",
+        "rev": "529c1a0b1f29f0d78fa3086b8f6a134c71ef3aaf",
+        "type": "github"
+      },
+      "original": {
+        "owner": "ipetkov",
+        "repo": "crane",
+        "type": "github"
+      }
+    },
     "flake-utils": {
       "inputs": {
         "systems": "systems"
@@ -19,27 +39,6 @@
         "type": "github"
       }
     },
-    "naersk": {
-      "inputs": {
-        "nixpkgs": [
-          "nixpkgs"
-        ]
-      },
-      "locked": {
-        "lastModified": 1718727675,
-        "narHash": "sha256-uFsCwWYI2pUpt0awahSBorDUrUfBhaAiyz+BPTS2MHk=",
-        "owner": "nix-community",
-        "repo": "naersk",
-        "rev": "941ce6dc38762a7cfb90b5add223d584feed299b",
-        "type": "github"
-      },
-      "original": {
-        "owner": "nix-community",
-        "ref": "master",
-        "repo": "naersk",
-        "type": "github"
-      }
-    },
     "nixpkgs": {
       "locked": {
         "lastModified": 1720031269,
@@ -58,8 +57,8 @@
     },
     "root": {
       "inputs": {
+        "crane": "crane",
         "flake-utils": "flake-utils",
-        "naersk": "naersk",
         "nixpkgs": "nixpkgs"
       }
     },
diff --git a/flake.nix b/flake.nix
index c844c23..77b518c 100644
--- a/flake.nix
+++ b/flake.nix
@@ -6,11 +6,8 @@
       repo = "flake-utils";
       ref = "main";
     };
-    naersk = {
-      type = "github";
-      owner = "nix-community";
-      repo = "naersk";
-      ref = "master";
+    crane = {
+      url = "github:ipetkov/crane";
       inputs.nixpkgs.follows = "nixpkgs";
     };
     nixpkgs = {
@@ -20,32 +17,43 @@
       ref = "nixos-unstable";
     };
   };
-  outputs = { self, nixpkgs, flake-utils, naersk }: let
+  outputs = { self, nixpkgs, flake-utils, crane }: let
     supportedSystems = ["aarch64-linux" "x86_64-linux"];
     forAllSystems = f: flake-utils.lib.eachSystem supportedSystems f;
   in {
     nixosModules.default = import ./configuration.nix self;
   } // forAllSystems (system: let
     pkgs = nixpkgs.legacyPackages.${system};
+    crane' = crane.mkLib pkgs;
+
+    kittybox = pkgs.callPackage ./kittybox.nix {
+      # TODO: this may break cross-compilation. It may be better to
+      # inject it as an overlay. However, I am unsure whether Crane
+      # can recognize it's being passed a cross-compilation set.
+      crane = crane';
+
+      nixosTests = {
+        smokeTest = self.checks.${system}.nixos-test;
+        postgresSmokeTest = self.checks.${system}.nixos-test-postgres;
+      };
+    };
   in {
     packages = {
-      kittybox = pkgs.callPackage ./kittybox.nix {
-        naersk = naersk.lib.${system};
-        inherit (pkgs.nodePackages) typescript;
-        nixosTests = {
-          smokeTest = self.checks.${system}.nixos-test;
-          postgresSmokeTest = self.checks.${system}.nixos-test-postgres;
-        };
-      };
+      kittybox = kittybox;
       default = self.packages.${system}.kittybox;
     };
 
     checks = {
       kittybox = self.packages.${system}.kittybox;
+
+      deps = self.packages.${system}.kittybox.cargoArtifacts;
+      clippy = self.packages.${system}.kittybox.clippy;
+
       distributed-test = pkgs.nixosTest (import ./nixos-tests/distributed-test.nix self);
       nixos-test = pkgs.nixosTest (import ./nixos-tests/smoke-test.nix self);
       nixos-test-postgres = pkgs.nixosTest (import ./nixos-tests/postgres-smoke-test.nix self);
       webmention-test = pkgs.nixosTest (import ./nixos-tests/webmention-test.nix self);
+
       dockerContainer = pkgs.callPackage ./docker.nix {
         inherit (self.packages.${system}) kittybox;
         rev = self.rev or "development";
diff --git a/kittybox.nix b/kittybox.nix
index 1d66fc7..de14cb7 100644
--- a/kittybox.nix
+++ b/kittybox.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, naersk, lld, mold, typescript
+{ crane, lib, nodePackages
 , useWebAuthn ? false, openssl, zlib, pkg-config, protobuf
 , usePostgres ? true, postgresql, postgresqlTestHook
 , nixosTests }:
@@ -8,27 +8,49 @@ assert usePostgres -> postgresql != null && postgresqlTestHook != null;
 
 let
   featureMatrix = features: lib.concatStringsSep " " (lib.attrNames (lib.filterAttrs (k: v: v) features));
-in
-naersk.buildPackage rec {
-  pname = "kittybox";
-  version = "0.1.0";
 
-  src = lib.sources.sourceFilesBySuffices ./. [".rs" ".toml" ".sql" ".ts" ".css" ".html" ".lock" ".json"];
+  suffixes = [ ".sql" ".ts" ".css" ".html" ".json" ];
+  suffixFilter = suffixes: name: type:
+      let base = baseNameOf (toString name);
+      in type == "directory" || lib.any (ext: lib.hasSuffix ext base) suffixes;
+  srcFilter = path: type: (suffixFilter suffixes path type) || (crane.filterCargoSources path type);
 
-  doCheck = stdenv.hostPlatform == stdenv.targetPlatform;
-  cargoBuildOptions = x: x ++ [
-    "--no-default-features"
-    "--features=\"${featureMatrix { webauthn = useWebAuthn; postgres = usePostgres; rustls = !useWebAuthn; }}\""
-  ];
-  cargoTestOptions = cargoBuildOptions;
+  src = lib.cleanSourceWith {
+    src = ./.; # The original, unfiltered source
+    filter = srcFilter;
+    name = "source"; # Be reproducible, regardless of the directory name
+  };
+
+  cargoFeatures = "--no-default-features --features=\"${featureMatrix {
+    webauthn = useWebAuthn;
+    postgres = usePostgres;
+    rustls   = !useWebAuthn;
+  }}\"";
 
-  buildInputs = lib.optional useWebAuthn openssl;
-  nativeBuildInputs = [ typescript ] ++ (lib.optional useWebAuthn pkg-config);
+  args = {
+    inherit src;
+    strictDeps = true;
 
+    cargoExtraArgs = cargoFeatures;
+
+    buildInputs = lib.optional useWebAuthn openssl;
+    nativeBuildInputs = [ nodePackages.typescript ] ++ (lib.optional useWebAuthn pkg-config);
+
+    meta = with lib.meta; {
+      maintainers = with lib.maintainers; [ vikanezrimaya ];
+      platforms = ["aarch64-linux" "x86_64-linux"];
+      mainProgram = "kittybox";
+    };
+  };
+
+  cargoArtifacts = crane.buildDepsOnly args;
+  args' = args // { inherit cargoArtifacts; };
+
+in crane.buildPackage (args' // {
   nativeCheckInputs = lib.optionals usePostgres [
     postgresql postgresqlTestHook
   ];
-
+  
   # Tests create arbitrary databases; we need to be prepared for that
   postgresqlTestUserOptions = "LOGIN SUPERUSER";
   postgresqlTestSetupPost = ''
@@ -38,11 +60,9 @@ naersk.buildPackage rec {
   passthru = {
     tests = nixosTests;
     hasPostgres = usePostgres;
-  };
 
-  meta = with lib.meta; {
-    maintainers = with lib.maintainers; [ vikanezrimaya ];
-    platforms = ["aarch64-linux" "x86_64-linux"];
-    mainProgram = "kittybox";
+    inherit cargoArtifacts;
+
+    clippy = crane.cargoClippy args';
   };
-}
+})