Add cleanCargoToml

This commit is contained in:
Ivan Petkov 2021-12-29 16:03:39 -08:00
parent e6a6ad68b1
commit a286fe1ff5
No known key found for this signature in database
GPG Key ID: BB6F9EFC065832B6
5 changed files with 127 additions and 4 deletions

View File

@ -1,5 +1,20 @@
{ {
"nodes": { "nodes": {
"nix-std": {
"locked": {
"lastModified": 1624642189,
"narHash": "sha256-tIt3p1k0PsYeIxK2xO9Z8k2Ggq2nH4auqj3RbVAiRrg=",
"owner": "chessai",
"repo": "nix-std",
"rev": "9ef1cf73948bfad05d370165c7f3011b82c4d679",
"type": "github"
},
"original": {
"owner": "chessai",
"repo": "nix-std",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1640269308, "lastModified": 1640269308,
@ -18,6 +33,7 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"nix-std": "nix-std",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"utils": "utils" "utils": "utils"
} }

View File

@ -1,13 +1,14 @@
{ {
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
nix-std.url = "github:chessai/nix-std";
utils = { utils = {
url = "github:numtide/flake-utils"; url = "github:numtide/flake-utils";
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
}; };
}; };
outputs = inputs@{ self, nixpkgs, utils, ... }: outputs = inputs@{ self, nixpkgs, nix-std, utils, ... }:
let let
myPkgsFor = pkgs: pkgs.callPackages ./pkgs { }; myPkgsFor = pkgs: pkgs.callPackages ./pkgs { };
in in
@ -23,6 +24,7 @@
# To override do: lib.overrideScope' (self: super: { ... }); # To override do: lib.overrideScope' (self: super: { ... });
lib = import ./lib { lib = import ./lib {
inherit (nix-std.lib.serde) fromTOML toTOML;
inherit (pkgs) lib newScope; inherit (pkgs) lib newScope;
inherit myPkgs; inherit myPkgs;
}; };

101
lib/cleanCargoToml.nix Normal file
View File

@ -0,0 +1,101 @@
{ fromTOML
, toTOML
, writeText
}:
let
dummyLib = writeText "lib.rs" "#[test] fn it_works() {}";
dummyMain = writeText "main.rs" "fn main() {}";
# https://doc.rust-lang.org/cargo/reference/manifest.html#the-package-section
cleanPackage = package: removeAttrs package [
"authors"
"autobenches"
"autobins"
"autoexamples"
"autotests"
"build"
"categories"
"default-run"
"description"
"documentation"
"edition"
"exclude"
"homepage"
"include"
"keywords"
"license-file"
"license"
"links"
"metadata"
"publish"
"readme"
"repository"
"resolver"
"rust-version"
# Additional package attributes which are expressly kept in
# (but listed here for audit purposes)
# "name" # The name of the package.
# "version" # The version of the package.
# "workspace" # Path to the workspace for the package.
];
# https://doc.rust-lang.org/cargo/reference/cargo-targets.html#configuring-a-target
cleanTargetCommon = pathReplacement: target:
let
cleanedCommon =
removeAttrs target [
"test"
"doctest"
"bench"
"doc"
"edition"
"plugin"
"proc-macro"
"harness"
"crate-type"
# Additional package attributes which are expressly kept in
# (but listed here for audit purposes)
# "name" # let cargo manage targets/collisions/etc.
# "required-features" # influences dependency feature combinations
];
in
cleanedCommon // { path = builtins.toString pathReplacement; };
# https://doc.rust-lang.org/cargo/reference/manifest.html
cleanCargoToml = parsed:
let
topLevelCleaned = removeAttrs parsed [
"badges" # Badges to display on a registry.
# Top level attributes intentionally left in place:
# "build-dependencies" # we want to build and cache these
# "cargo-features" # just in case some special depencency-related features are needed
# "dependencies" # we want build and cache these
# "dev-dependencies" # we want to build and cache these
# "features" # keep this as is, some deps may be compiled with different feature combinations
# "patch" # configures sources as the project wants
# "profile" # this could influence how dependencies are built/optimized
# "replace" # (deprecated) configures sources as the project wants
# "target" # we want to build and cache these
# "workspace" # keep the workspace hierarchy as the project wants
];
recursivelyCleaned = {
package = cleanPackage (parsed.package or { });
lib = cleanTargetCommon dummyLib (parsed.lib or { });
bench = map (cleanTargetCommon dummyLib) (parsed.bench or [ ]);
bin = map (cleanTargetCommon dummyMain) (parsed.bin or [ ]);
example = map (cleanTargetCommon dummyLib) (parsed.example or [ ]);
test = map (cleanTargetCommon dummyLib) (parsed.test or [ ]);
};
in
topLevelCleaned // recursivelyCleaned;
in
{ cargoToml ? throw "either cargoToml or cargoTomlContents must be specified"
, cargoTomlContents ? builtins.readFile cargoToml
}:
cleanCargoToml (fromTOML cargoTomlContents)

View File

@ -1,6 +1,8 @@
{ lib { fromTOML
, lib
, myPkgs , myPkgs
, newScope , newScope
, toTOML
}: }:
lib.makeScope newScope (self: lib.makeScope newScope (self:
@ -8,9 +10,10 @@ lib.makeScope newScope (self:
callPackage = self.newScope { }; callPackage = self.newScope { };
in in
myPkgs // { myPkgs // {
inherit callPackage; inherit callPackage fromTOML toTOML;
buildWithCargo = callPackage ./buildWithCargo.nix { }; buildWithCargo = callPackage ./buildWithCargo.nix { };
cleanCargoToml = callPackage ./cleanCargoToml.nix { };
downloadCargoPackage = callPackage ./downloadCargoPackage.nix { }; downloadCargoPackage = callPackage ./downloadCargoPackage.nix { };
urlForCargoPackage = callPackage ./urlForCargoPackage.nix { }; urlForCargoPackage = callPackage ./urlForCargoPackage.nix { };
vendorCargoDeps = callPackage ./vendorCargoDeps.nix { }; vendorCargoDeps = callPackage ./vendorCargoDeps.nix { };

View File

@ -1,4 +1,5 @@
{ downloadCargoPackage { downloadCargoPackage
, fromTOML
, linkFarm , linkFarm
}: }:
@ -6,7 +7,7 @@
, cargoLockContents ? builtins.readFile cargoLock , cargoLockContents ? builtins.readFile cargoLock
}: }:
let let
lock = builtins.fromTOML cargoLockContents; lock = fromTOML cargoLockContents;
packages = packages =
if lock ? package if lock ? package