Skip to content

Commit

Permalink
Extract a “pure” lib from mkLib
Browse files Browse the repository at this point in the history
The pure lib contains the parts of the Crane lib that don’t depend on
`pkgs`, and is exposed via the usual `lib` flake output.

The result of `mkLib` contains all of `lib`.

Fixes ipetkov#699.
  • Loading branch information
sellout committed Sep 23, 2024
1 parent 7e4586b commit be1f170
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 52 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
61 changes: 17 additions & 44 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,9 @@ let
scopeFn = self:
let
inherit (self) callPackage;

internalCrateNameFromCargoToml = callPackage ./internalCrateNameFromCargoToml.nix { };
internalCrateNameForCleanSource = callPackage ./internalCrateNameForCleanSource.nix {
inherit internalCrateNameFromCargoToml;
};
inherit (pureLib) registryFromDownloadUrl;
in
{
pureLib // {
appendCrateRegistries = input: self.overrideScope (_final: prev: {
crateRegistries = prev.crateRegistries // (lib.foldl (a: b: a // b) { } input);
});
Expand All @@ -65,28 +57,18 @@ 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 {
crateRegistries = 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,13 +92,6 @@ 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 { };
Expand All @@ -127,19 +102,17 @@ let
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.
43 changes: 43 additions & 0 deletions pure-lib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ 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;
};

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

registryFromDownloadUrl = import ./registryFromDownloadUrl.nix { inherit lib; };
in
{
inherit 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; };
};
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.
2 changes: 1 addition & 1 deletion test/flake.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
inputs = {
# NB: nixpkgs-unstable testing will come from the root flake
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
nixpkgs.follows = "crane/nixpkgs"
nixpkgs-latest-release.url = "github:NixOS/nixpkgs/release-24.05";
nixpkgs-darwin.url = "github:NixOS/nixpkgs/nixpkgs-24.05-darwin";

Expand Down

0 comments on commit be1f170

Please sign in to comment.