Skip to content

Commit

Permalink
Make all functions overridable
Browse files Browse the repository at this point in the history
Currently, most functions take `pkgs` as an argument, and fail to use some of their other arguments, effectively replacing them with `pkgs.[blah]`. This commit fixes that.
  • Loading branch information
P-E-Meunier committed Dec 17, 2024
1 parent 8537c2d commit 48b7e9b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 53 deletions.
22 changes: 14 additions & 8 deletions crate2nix/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
# See https://github.com/kolloch/crate2nix for more info.

{ nixpkgs ? ../nix/nixpkgs.nix
, pkgs ? import nixpkgs { config = {}; }
, pkgs ? import nixpkgs { }
, fetchurl ? pkgs.fetchurl
, fetchgit ? pkgs.fetchgit
, callPackage ? pkgs.callPackage
, lib ? pkgs.lib
, buildPackages ? pkgs.buildPackages
, stdenv ? pkgs.stdenv
, stdenvNoCC ? pkgs.stdenvNoCC
, symlinkJoin ? pkgs.symlinkJoin
, runCommand ? pkgs.runCommand
, buildRustCrateForPkgs ? pkgs: pkgs.buildRustCrate
# This is used as the `crateOverrides` argument for `buildRustCrate`.
, defaultCrateOverrides ? pkgs.defaultCrateOverrides
Expand All @@ -24,7 +30,7 @@
# Additional crate2nix configuration if it exists.
, crateConfig
? if builtins.pathExists ./crate-config.nix
then pkgs.callPackage ./crate-config.nix {}
then callPackage ./crate-config.nix {}
else {}
}:

Expand Down Expand Up @@ -65,7 +71,7 @@ rec {


# A derivation that joins the outputs of all workspace members together.
allWorkspaceMembers = pkgs.symlinkJoin {
allWorkspaceMembers = symlinkJoin {
name = "all-workspace-members";
paths =
let members = builtins.attrValues workspaceMembers;
Expand Down Expand Up @@ -3286,15 +3292,15 @@ rec {
# If the user hasn't set any pre/post commands, we don't want to
# insert empty lines. This means that any existing users of crate2nix
# don't get a spurious rebuild unless they set these explicitly.
testCommand = pkgs.lib.concatStringsSep "\n" (
pkgs.lib.filter (s: s != "") [
testCommand = lib.concatStringsSep "\n" (
lib.filter (s: s != "") [
testPreRun
"$f $testCrateFlags 2>&1 | tee -a $out"
testPostRun
]
);
in
pkgs.stdenvNoCC.mkDerivation {
stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

inherit (crate) src;
Expand Down Expand Up @@ -3330,7 +3336,7 @@ rec {
'';
};
in
pkgs.runCommand "${crate.name}-linked"
runCommand "${crate.name}-linked"
{
inherit (crate) outputs crateName;
passthru = (crate.passthru or { }) // {
Expand Down Expand Up @@ -3469,7 +3475,7 @@ rec {
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
build = mkBuiltByPackageIdByPkgs buildPackages;
};
in
self;
Expand Down
20 changes: 13 additions & 7 deletions crate2nix/templates/Cargo.nix.tera
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
# See https://github.com/kolloch/crate2nix for more info.

{ nixpkgs ? {{config.nixpkgs_path | safe}}
, pkgs ? import nixpkgs { config = {}; }
, pkgs ? import nixpkgs { }
, fetchurl ? pkgs.fetchurl
, fetchgit ? pkgs.fetchgit
, callPackage ? pkgs.callPackage
, lib ? pkgs.lib
, buildPackages ? pkgs.buildPackages
, stdenv ? pkgs.stdenv
, stdenvNoCC ? pkgs.stdenvNoCC
, symlinkJoin ? pkgs.symlinkJoin
, runCommand ? pkgs.runCommand
, buildRustCrateForPkgs ? pkgs: pkgs.buildRustCrate
# This is used as the `crateOverrides` argument for `buildRustCrate`.
, defaultCrateOverrides ? pkgs.defaultCrateOverrides
Expand All @@ -26,7 +32,7 @@
# Additional crate2nix configuration if it exists.
, crateConfig
? if builtins.pathExists ./crate-config.nix
then pkgs.callPackage ./crate-config.nix {}
then callPackage ./crate-config.nix {}
else {}
}:

Expand Down Expand Up @@ -74,15 +80,15 @@ rec {
registries = {
{%- for name, hash in registries %}
{{name}} = builtins.fromJSON (builtins.readFile (fetchurl {
url = "{{name | safe}}/config.json";
url = "{{name | trim_end_matches(pat="/") | safe}}/config.json";
sha256 = {{hash}};
}));
{%- endfor %}
};
{%- endif %}

# A derivation that joins the outputs of all workspace members together.
allWorkspaceMembers = pkgs.symlinkJoin {
allWorkspaceMembers = symlinkJoin {
name = "all-workspace-members";
paths =
let members = builtins.attrValues workspaceMembers;
Expand Down Expand Up @@ -147,16 +153,16 @@ rec {
{%- elif crate.source.Nix.file.import and crate.source.Nix.attr %}
src = (import {{crate.source.Nix.file.import | safe}}).{{crate.source.Nix.attr | safe}};
{%- elif crate.source.Nix.file.package and crate.source.Nix.attr %}
src = (pkgs.callPackage {{crate.source.Nix.file.package | safe}} {}).{{crate.source.Nix.attr | safe}};
src = (callPackage {{crate.source.Nix.file.package | safe}} {}).{{crate.source.Nix.attr | safe}};
{%- elif crate.source.Nix.file.import %}
src = import {{crate.source.Nix.file.import | safe}};
{%- elif crate.source.Nix.file.package %}
src = pkgs.callPackage {{crate.source.Nix.file.package | safe}} {};
src = callPackage {{crate.source.Nix.file.package | safe}} {};
{%- elif crate.source.LocalDirectory.path %}
src = lib.cleanSourceWith { filter = sourceFilter; src = {{crate.source.LocalDirectory.path | safe}}; };
{%- elif crate.source.Git %}
workspace_member = null;
src = pkgs.fetchgit {
src = fetchgit {
url = {{crate.source.Git.url}};
rev = {{crate.source.Git.rev}};
{%- if crate.source.Git.sha256 %}
Expand Down
17 changes: 10 additions & 7 deletions crate2nix/templates/nix/crate2nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#
# crate2nix/default.nix (excerpt start)
#{#
{ pkgs
, lib
{ lib
, pkgs
, stdenv
, stdenvNoCC
, buildRustCrate
, buildRustCrateForPkgs ? if buildRustCrate != null then
lib.warn "`buildRustCrate` is deprecated, use `buildRustCrateForPkgs` instead" (_: buildRustCrate)
else
pkgs: pkgs.buildRustCrate
, buildPackages
, defaultCrateOverrides
, runCommand
, strictDeprecation ? true
, crates ? { }
, rootFeatures ? [ ]
Expand Down Expand Up @@ -157,15 +160,15 @@ rec {
# If the user hasn't set any pre/post commands, we don't want to
# insert empty lines. This means that any existing users of crate2nix
# don't get a spurious rebuild unless they set these explicitly.
testCommand = pkgs.lib.concatStringsSep "\n" (
pkgs.lib.filter (s: s != "") [
testCommand = lib.concatStringsSep "\n" (
lib.filter (s: s != "") [
testPreRun
"$f $testCrateFlags 2>&1 | tee -a $out"
testPostRun
]
);
in
pkgs.stdenvNoCC.mkDerivation {
stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

inherit (crate) src;
Expand Down Expand Up @@ -201,7 +204,7 @@ rec {
'';
};
in
pkgs.runCommand "${crate.name}-linked"
runCommand "${crate.name}-linked"
{
inherit (crate) outputs crateName;
passthru = (crate.passthru or { }) // {
Expand Down Expand Up @@ -340,7 +343,7 @@ rec {
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
build = mkBuiltByPackageIdByPkgs buildPackages;
};
in
self;
Expand Down
26 changes: 16 additions & 10 deletions sample_projects/bin_with_git_submodule_dep/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
# See https://github.com/kolloch/crate2nix for more info.

{ nixpkgs ? <nixpkgs>
, pkgs ? import nixpkgs { config = {}; }
, pkgs ? import nixpkgs { }
, fetchurl ? pkgs.fetchurl
, fetchgit ? pkgs.fetchgit
, callPackage ? pkgs.callPackage
, lib ? pkgs.lib
, buildPackages ? pkgs.buildPackages
, stdenv ? pkgs.stdenv
, stdenvNoCC ? pkgs.stdenvNoCC
, symlinkJoin ? pkgs.symlinkJoin
, runCommand ? pkgs.runCommand
, buildRustCrateForPkgs ? pkgs: pkgs.buildRustCrate
# This is used as the `crateOverrides` argument for `buildRustCrate`.
, defaultCrateOverrides ? pkgs.defaultCrateOverrides
Expand All @@ -24,7 +30,7 @@
# Additional crate2nix configuration if it exists.
, crateConfig
? if builtins.pathExists ./crate-config.nix
then pkgs.callPackage ./crate-config.nix {}
then callPackage ./crate-config.nix {}
else {}
}:

Expand Down Expand Up @@ -65,7 +71,7 @@ rec {


# A derivation that joins the outputs of all workspace members together.
allWorkspaceMembers = pkgs.symlinkJoin {
allWorkspaceMembers = symlinkJoin {
name = "all-workspace-members";
paths =
let members = builtins.attrValues workspaceMembers;
Expand Down Expand Up @@ -485,7 +491,7 @@ rec {
edition = "2018";
links = "rocksdb";
workspace_member = null;
src = pkgs.fetchgit {
src = fetchgit {
url = "https://github.com/rust-rocksdb/rust-rocksdb";
rev = "66f04df013b6e6bd42b5a8c353406e09a7c7da2a";
sha256 = "1rchvjrjamdaznx26gy4bmjj10rrf00mgc1wvkc489r9z1nh4h1h";
Expand Down Expand Up @@ -837,7 +843,7 @@ rec {
version = "0.21.0";
edition = "2018";
workspace_member = null;
src = pkgs.fetchgit {
src = fetchgit {
url = "https://github.com/rust-rocksdb/rust-rocksdb";
rev = "66f04df013b6e6bd42b5a8c353406e09a7c7da2a";
sha256 = "1rchvjrjamdaznx26gy4bmjj10rrf00mgc1wvkc489r9z1nh4h1h";
Expand Down Expand Up @@ -1504,15 +1510,15 @@ rec {
# If the user hasn't set any pre/post commands, we don't want to
# insert empty lines. This means that any existing users of crate2nix
# don't get a spurious rebuild unless they set these explicitly.
testCommand = pkgs.lib.concatStringsSep "\n" (
pkgs.lib.filter (s: s != "") [
testCommand = lib.concatStringsSep "\n" (
lib.filter (s: s != "") [
testPreRun
"$f $testCrateFlags 2>&1 | tee -a $out"
testPostRun
]
);
in
pkgs.stdenvNoCC.mkDerivation {
stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

inherit (crate) src;
Expand Down Expand Up @@ -1548,7 +1554,7 @@ rec {
'';
};
in
pkgs.runCommand "${crate.name}-linked"
runCommand "${crate.name}-linked"
{
inherit (crate) outputs crateName;
passthru = (crate.passthru or { }) // {
Expand Down Expand Up @@ -1687,7 +1693,7 @@ rec {
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
build = mkBuiltByPackageIdByPkgs buildPackages;
};
in
self;
Expand Down
28 changes: 17 additions & 11 deletions sample_projects/codegen/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
# See https://github.com/kolloch/crate2nix for more info.

{ nixpkgs ? <nixpkgs>
, pkgs ? import nixpkgs { config = {}; }
, pkgs ? import nixpkgs { }
, fetchurl ? pkgs.fetchurl
, fetchgit ? pkgs.fetchgit
, callPackage ? pkgs.callPackage
, lib ? pkgs.lib
, buildPackages ? pkgs.buildPackages
, stdenv ? pkgs.stdenv
, stdenvNoCC ? pkgs.stdenvNoCC
, symlinkJoin ? pkgs.symlinkJoin
, runCommand ? pkgs.runCommand
, buildRustCrateForPkgs ? pkgs: pkgs.buildRustCrate
# This is used as the `crateOverrides` argument for `buildRustCrate`.
, defaultCrateOverrides ? pkgs.defaultCrateOverrides
Expand All @@ -24,7 +30,7 @@
# Additional crate2nix configuration if it exists.
, crateConfig
? if builtins.pathExists ./crate-config.nix
then pkgs.callPackage ./crate-config.nix {}
then callPackage ./crate-config.nix {}
else {}
}:

Expand Down Expand Up @@ -65,7 +71,7 @@ rec {


# A derivation that joins the outputs of all workspace members together.
allWorkspaceMembers = pkgs.symlinkJoin {
allWorkspaceMembers = symlinkJoin {
name = "all-workspace-members";
paths =
let members = builtins.attrValues workspaceMembers;
Expand Down Expand Up @@ -249,7 +255,7 @@ rec {
version = "0.9.7";
edition = "2018";
workspace_member = null;
src = pkgs.fetchgit {
src = fetchgit {
url = "https://github.com/diwic/dbus-rs.git";
rev = "618262f5e3217cdd173d46d705bbac26c5141e21";
sha256 = "0gvhz2knd1k799l7ssh4rdm5qw0vhazzr3bxpmlgq7fhy6hjazrs";
Expand Down Expand Up @@ -287,7 +293,7 @@ rec {
edition = "2018";
crateBin = [];
workspace_member = null;
src = pkgs.fetchgit {
src = fetchgit {
url = "https://github.com/diwic/dbus-rs.git";
rev = "618262f5e3217cdd173d46d705bbac26c5141e21";
sha256 = "0gvhz2knd1k799l7ssh4rdm5qw0vhazzr3bxpmlgq7fhy6hjazrs";
Expand Down Expand Up @@ -362,7 +368,7 @@ rec {
edition = "2015";
links = "dbus";
workspace_member = null;
src = pkgs.fetchgit {
src = fetchgit {
url = "https://github.com/diwic/dbus-rs.git";
rev = "618262f5e3217cdd173d46d705bbac26c5141e21";
sha256 = "0gvhz2knd1k799l7ssh4rdm5qw0vhazzr3bxpmlgq7fhy6hjazrs";
Expand Down Expand Up @@ -679,15 +685,15 @@ rec {
# If the user hasn't set any pre/post commands, we don't want to
# insert empty lines. This means that any existing users of crate2nix
# don't get a spurious rebuild unless they set these explicitly.
testCommand = pkgs.lib.concatStringsSep "\n" (
pkgs.lib.filter (s: s != "") [
testCommand = lib.concatStringsSep "\n" (
lib.filter (s: s != "") [
testPreRun
"$f $testCrateFlags 2>&1 | tee -a $out"
testPostRun
]
);
in
pkgs.stdenvNoCC.mkDerivation {
stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

inherit (crate) src;
Expand Down Expand Up @@ -723,7 +729,7 @@ rec {
'';
};
in
pkgs.runCommand "${crate.name}-linked"
runCommand "${crate.name}-linked"
{
inherit (crate) outputs crateName;
passthru = (crate.passthru or { }) // {
Expand Down Expand Up @@ -862,7 +868,7 @@ rec {
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
build = mkBuiltByPackageIdByPkgs buildPackages;
};
in
self;
Expand Down
Loading

0 comments on commit 48b7e9b

Please sign in to comment.