Skip to content

Commit

Permalink
Extract a “pure” lib from mkLib
Browse files Browse the repository at this point in the history
This contains the parts of the Crane lib that don’t depend on `pkgs`.

Fixes ipetkov#699.
  • Loading branch information
sellout committed Sep 22, 2024
1 parent 7e4586b commit d1600fc
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 55 deletions.
6 changes: 4 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{ pkgs ? import <nixpkgs> { } }:
{ pkgs ? import <nixpkgs> { }
, pureLib ? import ./pure-lib { inherit (pkgs) lib; }
}:

pkgs.callPackage ./lib { }
pkgs.callPackage ./lib { inherit pureLib; }
22 changes: 21 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
description = "A Nix library for building cargo projects. Never build twice thanks to incremental artifact caching.";

inputs = { };
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

nixConfig = {
extra-substituters = [ "https://crane.cachix.org" ];
extra-trusted-public-keys = [ "crane.cachix.org-1:8Scfpmn9w+hGdXH/Q9tTLiYAE/2dnJYRJP7kl80GuRk=" ];
};

outputs = { ... }:
outputs = { nixpkgs, ... }:
let
lib = import ./pure-lib { inherit (nixpkgs) lib; };
mkLib = pkgs: import ./default.nix {
pureLib = lib;
inherit pkgs;
};
nodes = (builtins.fromJSON (builtins.readFile ./test/flake.lock)).nodes;
Expand Down Expand Up @@ -48,7 +50,7 @@
];
in
{
inherit mkLib;
inherit lib mkLib;

overlays.default = _final: _prev: { };

Expand Down Expand Up @@ -121,7 +123,6 @@
};
} // eachDefaultSystem (system:
let
nixpkgs = inputFromLock "nixpkgs";
pkgs = import nixpkgs {
inherit system;
};
Expand Down
63 changes: 15 additions & 48 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ lib
, pureLib
, stdenv
, makeScopeWithSplicing'
, splicePackages
Expand Down Expand Up @@ -40,17 +41,8 @@ let
scopeFn = self:
let
inherit (self) callPackage;

internalCrateNameFromCargoToml = callPackage ./internalCrateNameFromCargoToml.nix { };
internalCrateNameForCleanSource = callPackage ./internalCrateNameForCleanSource.nix {
inherit internalCrateNameFromCargoToml;
};
in
{
appendCrateRegistries = input: self.overrideScope (_final: prev: {
crateRegistries = prev.crateRegistries // (lib.foldl (a: b: a // b) { } input);
});

pureLib // {
buildDepsOnly = callPackage ./buildDepsOnly.nix { };
buildPackage = callPackage ./buildPackage.nix { };
buildTrunkPackage = callPackage ./buildTrunkPackage.nix { };
Expand All @@ -65,28 +57,13 @@ let
cargoNextest = callPackage ./cargoNextest.nix { };
cargoTarpaulin = callPackage ./cargoTarpaulin.nix { };
cargoTest = callPackage ./cargoTest.nix { };
cleanCargoSource = callPackage ./cleanCargoSource.nix {
inherit internalCrateNameForCleanSource;
};
cleanCargoToml = callPackage ./cleanCargoToml.nix { };
configureCargoCommonVarsHook = callPackage ./setupHooks/configureCargoCommonVars.nix { };
configureCargoVendoredDepsHook = callPackage ./setupHooks/configureCargoVendoredDeps.nix { };
craneUtils = callPackage ../pkgs/crane-utils { };

crateNameFromCargoToml = callPackage ./crateNameFromCargoToml.nix {
inherit internalCrateNameFromCargoToml;
};

crateRegistries = self.registryFromDownloadUrl {
dl = "https://static.crates.io/crates";
indexUrl = "https://github.com/rust-lang/crates.io-index";
};

devShell = callPackage ./devShell.nix { };
downloadCargoPackage = callPackage ./downloadCargoPackage.nix { };
downloadCargoPackageFromGit = callPackage ./downloadCargoPackageFromGit.nix { };
filterCargoSources = callPackage ./filterCargoSources.nix { };
findCargoFiles = callPackage ./findCargoFiles.nix { };
inheritCargoArtifactsHook = callPackage ./setupHooks/inheritCargoArtifacts.nix { };
installCargoArtifactsHook = callPackage ./setupHooks/installCargoArtifacts.nix { };
installFromCargoBuildLogHook = callPackage ./setupHooks/installFromCargoBuildLog.nix { };
Expand All @@ -110,36 +87,26 @@ let
lib.warnIf needsSplicing warningMsg (lib.genAttrs attrsForToolchainOverride (_: toolchain))
);

path = callPackage ./path.nix {
inherit internalCrateNameForCleanSource;
};

registryFromDownloadUrl = callPackage ./registryFromDownloadUrl.nix { };
registryFromGitIndex = callPackage ./registryFromGitIndex.nix { };
registryFromSparse = callPackage ./registryFromSparse.nix { };
removeReferencesToVendoredSourcesHook = callPackage ./setupHooks/removeReferencesToVendoredSources.nix { };
replaceCargoLockHook = callPackage ./setupHooks/replaceCargoLockHook.nix { };
taploFmt = callPackage ./taploFmt.nix { };
urlForCargoPackage = callPackage ./urlForCargoPackage.nix { };
vendorCargoDeps = callPackage ./vendorCargoDeps.nix { };
vendorMultipleCargoDeps = callPackage ./vendorMultipleCargoDeps.nix { };
vendorCargoRegistries = callPackage ./vendorCargoRegistries.nix { };
vendorGitDeps = callPackage ./vendorGitDeps.nix { };
writeTOML = callPackage ./writeTOML.nix { };
};

craneSpliced = makeScopeWithSplicing' {
f = scopeFn;
otherSplices = {
selfBuildBuild = lib.makeScope pkgsBuildBuild.newScope scopeFn;
selfBuildHost = lib.makeScope pkgsBuildHost.newScope scopeFn;
selfBuildTarget = lib.makeScope pkgsBuildTarget.newScope scopeFn;
selfHostHost = lib.makeScope pkgsHostHost.newScope scopeFn;
selfHostTarget = lib.makeScope pkgsHostTarget.newScope scopeFn;
selfTargetTarget = lib.optionalAttrs (pkgsTargetTarget?newScope) (lib.makeScope pkgsTargetTarget.newScope scopeFn);
};
keep = self: lib.optionalAttrs (self?cargo)
(lib.genAttrs attrsForToolchainOverride (n: self.${n}));
};
in
lib.warnIf isUnsupported msg (craneSpliced)
makeScopeWithSplicing' {
f = scopeFn;
otherSplices = {
selfBuildBuild = lib.makeScope pkgsBuildBuild.newScope scopeFn;
selfBuildHost = lib.makeScope pkgsBuildHost.newScope scopeFn;
selfBuildTarget = lib.makeScope pkgsBuildTarget.newScope scopeFn;
selfHostHost = lib.makeScope pkgsHostHost.newScope scopeFn;
selfHostTarget = lib.makeScope pkgsHostTarget.newScope scopeFn;
selfTargetTarget = lib.optionalAttrs (pkgsTargetTarget?newScope) (lib.makeScope pkgsTargetTarget.newScope scopeFn);
};
keep = self: lib.optionalAttrs (self?cargo)
(lib.genAttrs attrsForToolchainOverride (n: self.${n}));
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions pure-lib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{ lib
}:

let
minSupported = "24.05";
current = lib.concatStringsSep "." (lib.lists.sublist 0 2 (lib.splitVersion lib.version));
isUnsupported = lib.versionOlder current minSupported;
msg = "crane requires at least nixpkgs-${minSupported}, supplied nixpkgs-${current}";

scopeFn = self:
let
internalCrateNameFromCargoToml = import ./internalCrateNameFromCargoToml.nix { inherit lib; };
internalCrateNameForCleanSource = import ./internalCrateNameForCleanSource.nix {
inherit internalCrateNameFromCargoToml;
};

filterCargoSources = import ./filterCargoSources.nix { inherit lib; };
registryFromDownloadUrl = import ./registryFromDownloadUrl.nix { inherit lib; };
crateRegistries = registryFromDownloadUrl {
dl = "https://static.crates.io/crates";
indexUrl = "https://github.com/rust-lang/crates.io-index";
};
in
{
inherit crateRegistries filterCargoSources registryFromDownloadUrl;

appendCrateRegistries = input: self.overrideScope (_final: prev: {
crateRegistries = prev.crateRegistries // (lib.foldl (a: b: a // b) { } input);
});

cleanCargoSource = import ./cleanCargoSource.nix {
inherit filterCargoSources internalCrateNameForCleanSource lib;
};
cleanCargoToml = import ./cleanCargoToml.nix { };

crateNameFromCargoToml = import ./crateNameFromCargoToml.nix {
inherit internalCrateNameFromCargoToml lib;
};

findCargoFiles = import ./findCargoFiles.nix { inherit lib; };

path = import ./path.nix {
inherit internalCrateNameForCleanSource lib;
};

registryFromGitIndex = import ./registryFromGitIndex.nix { inherit registryFromDownloadUrl; };
registryFromSparse = import ./registryFromSparse.nix { inherit registryFromDownloadUrl lib; };
urlForCargoPackage = import ./urlForCargoPackage.nix { inherit crateRegistries lib; };
};
craneLib = lib.makeScope lib.callPackage scopeFn;
in
lib.warnIf isUnsupported msg craneLib
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d1600fc

Please sign in to comment.