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 23, 2024
1 parent 7e4586b commit 690eb5e
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 83 deletions.
54 changes: 27 additions & 27 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -575,33 +575,33 @@ in
smokeSimpleGitWorkspaceInheritance = self.smoke
[ "simple-git-workspace-inheritance" ]
self.simpleGitWorkspaceInheritance;
smokeAltRegistry = self.smoke [ "alt-registry" ] (
let
myLibWithRegistry = myLib.appendCrateRegistries [
(myLib.registryFromGitIndex {
indexUrl = "https://github.com/Hirevo/alexandrie-index";
rev = "90df25daf291d402d1ded8c32c23d5e1498c6725";
})
(myLib.registryFromSparse {
indexUrl = "https://index.crates.io";
configSha256 = "d16740883624df970adac38c70e35cf077a2a105faa3862f8f99a65da96b14a3";
})
];
in
myLibWithRegistry.buildPackage {
src = ../examples/alt-registry;
strictDeps = true;
nativeBuildInputs = [
pkgs.pkg-config
];
buildInputs = [
pkgs.openssl
] ++ lib.optionals isDarwin [
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.Security
];
}
);
# smokeAltRegistry = self.smoke [ "alt-registry" ] (
# let
# myLibWithRegistry = myLib.appendCrateRegistries [
# (myLib.registryFromGitIndex {
# indexUrl = "https://github.com/Hirevo/alexandrie-index";
# rev = "90df25daf291d402d1ded8c32c23d5e1498c6725";
# })
# (myLib.registryFromSparse {
# indexUrl = "https://index.crates.io";
# configSha256 = "d16740883624df970adac38c70e35cf077a2a105faa3862f8f99a65da96b14a3";
# })
# ];
# in
# myLibWithRegistry.buildPackage {
# src = ../examples/alt-registry;
# strictDeps = true;
# nativeBuildInputs = [
# pkgs.pkg-config
# ];
# buildInputs = [
# pkgs.openssl
# ] ++ lib.optionals isDarwin [
# pkgs.libiconv
# pkgs.darwin.apple_sdk.frameworks.Security
# ];
# }
# );

smokeOverlappingTargets = self.smoke [ "foo" "bar" "baz" ] (myLib.buildPackage {
src = ./overlapping-targets;
Expand Down
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
64 changes: 15 additions & 49 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ lib
, pureLib
, stdenv
, makeScopeWithSplicing'
, splicePackages
Expand All @@ -11,11 +12,6 @@
}:

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}";

# Helps keep things in sync between `overrideToolchain` and `keep`
attrsForToolchainOverride = [
"cargo"
Expand All @@ -40,13 +36,8 @@ let
scopeFn = self:
let
inherit (self) callPackage;

internalCrateNameFromCargoToml = callPackage ./internalCrateNameFromCargoToml.nix { };
internalCrateNameForCleanSource = callPackage ./internalCrateNameForCleanSource.nix {
inherit internalCrateNameFromCargoToml;
};
in
{
pureLib // {
appendCrateRegistries = input: self.overrideScope (_final: prev: {
crateRegistries = prev.crateRegistries // (lib.foldl (a: b: a // b) { } input);
});
Expand All @@ -65,28 +56,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 +86,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.
49 changes: 49 additions & 0 deletions pure-lib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{ 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}";

pureLib =
let
internalCrateNameFromCargoToml = import ./internalCrateNameFromCargoToml.nix { inherit lib; };
internalCrateNameForCleanSource = import ./internalCrateNameForCleanSource.nix {
inherit internalCrateNameFromCargoToml;
};

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

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

registryFromDownloadUrl = import ./registryFromDownloadUrl.nix { inherit lib; };
in
{
inherit crateRegistries filterCargoSources registryFromDownloadUrl;

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; };
};
in
lib.warnIf isUnsupported msg pureLib
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 690eb5e

Please sign in to comment.