Skip to content

Commit

Permalink
[default.nix] Provide a callPackage-compatible default.nix with overr…
Browse files Browse the repository at this point in the history
…ides
  • Loading branch information
kolloch committed Jan 4, 2024
1 parent f3f4de5 commit 398aaf3
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 95 deletions.
85 changes: 85 additions & 0 deletions crate2nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

# Provided by callPackage or also directly usable via nix-build with defaults.
{
pkgs ? (
let
flakeLock = builtins.fromJSON (builtins.readFile ../flake.lock);
in
import "${builtins.fetchTree flakeLock.nodes.nixpkgs.locked}" { }
)
, stdenv ? pkgs.stdenv
, lib ? pkgs.lib
, symlinkJoin ? pkgs.symlinkJoin
, makeWrapper ? pkgs.makeWrapper
, darwin ? pkgs.darwin
, defaultCrateOverrides ? pkgs.defaultCrateOverrides
, nix ? pkgs.nix
, cargo ? pkgs.cargo
, callPackage ? pkgs.callPackage
, nix-prefetch-git ? pkgs.nix-prefetch-git
# Seperate arguements that are NOT filled by callPackage.
, cargoNixPath ? ./Cargo.nix, release ? true }:
let
cargoNix = callPackage cargoNixPath { inherit release; };
withoutTemplates = name: type:
let
baseName = builtins.baseNameOf (builtins.toString name);
in
!(baseName == "templates" && type == "directory");
crate2nix = cargoNix.rootCrate.build.override {
testCrateFlags = [
"--skip nix_integration_tests"
];
crateOverrides = defaultCrateOverrides // {
crate2nix = { src, ... }: {
src =
if release
then src
else
lib.cleanSourceWith {
filter = withoutTemplates;
inherit src;
};
dontFixup = !release;
};
cssparser-macros = attrs: assert builtins.trace "cssparser" true;{
buildInputs = lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security ];
};
};
};
set_templates = if release then "" else "--set TEMPLATES_DIR ${./templates}";
in symlinkJoin {
name = crate2nix.name;
paths = [ crate2nix ];
buildInputs = [ makeWrapper cargo ];
meta = {
description = "Nix build file generator for rust crates.";
longDescription = ''
Crate2nix generates nix files from Cargo.toml/lock files
so that you can build every crate individually in a nix sandbox.
'';
homepage = "https://github.com/nix-community/crate2nix";
license = lib.licenses.asl20;
maintainers = [
{
github = "kolloch";
githubId = 339354;
name = "Peter Kolloch";
}
lib.maintainers.andir
];
mainProgram = "crate2nix";
platforms = lib.platforms.all;
};
postBuild = ''
# Fallback to built dependencies for cargo and nix-prefetch-url
wrapProgram $out/bin/crate2nix ${set_templates}\
--suffix PATH ":" ${lib.makeBinPath [ cargo nix nix-prefetch-git ]}
rm -rf $out/lib $out/bin/crate2nix.d
mkdir -p \
$out/share/bash-completion/completions \
$out/share/zsh/vendor-completions
$out/bin/crate2nix completions -s 'bash' -o $out/share/bash-completion/completions
$out/bin/crate2nix completions -s 'zsh' -o $out/share/zsh/vendor-completions
'';
}
12 changes: 12 additions & 0 deletions crate2nix/flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
flake.overlays.default = final: prev: {
crate2nix = prev.callPackage ./default.nix { };
};

perSystem =
{ pkgs
, ...
}: {
packages.default = pkgs.callPackage ./default.nix {};
};
}
11 changes: 1 addition & 10 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
import ./crate2nix/default.nix
7 changes: 2 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
inputs = {
nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2311.553775.tar.gz";
nixpkgs.url = "nixpkgs/0cbe9f69c234a7700596e943bfae7ef27a31b735";

flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz";

Expand All @@ -27,13 +27,10 @@
imports = [
./nix/devshell/flake-module.nix
./nix/pre-commit/flake-module.nix
./nix/crate2nix/flake-module.nix
./crate2nix/flake-module.nix
];

flake = {
overlays.default = final: prev: {
crate2nix = self.callPackage ./default.nix { };
};
templates.default = {
path = ./template;
description = "An example of crate2nix";
Expand Down
78 changes: 0 additions & 78 deletions nix/crate2nix/flake-module.nix

This file was deleted.

2 changes: 1 addition & 1 deletion nix/devshell/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
];

language.c = {
libraries = lib.optional pkgs.stdenv.isDarwin pkgs.libiconv;
libraries = lib.optional pkgs.stdenv.isDarwin pkgs.libiconv;
};

env = [
Expand Down
2 changes: 1 addition & 1 deletion tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
, stdenv ? pkgs.stdenv
}:
let
crate2nix = (import ./default.nix).default;
crate2nix = pkgs.callPackage ./default.nix { };
nixTest = pkgs.callPackage ./nix/nix-test-runner.nix { };
nodes = {
dev = { pkgs, ... }: {
Expand Down

0 comments on commit 398aaf3

Please sign in to comment.