refactor: clean up scripts
Tidy up the script logic and deduplicate it a bit.
This commit is contained in:
parent
3977e8d50e
commit
d4997ba0c1
3 changed files with 47 additions and 72 deletions
|
@ -1,31 +1,13 @@
|
||||||
{ lib, rustPlatform }:
|
{
|
||||||
|
lib,
|
||||||
|
rustPlatform,
|
||||||
|
cleanRustSrc,
|
||||||
|
}:
|
||||||
let
|
let
|
||||||
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
||||||
inherit (cargoToml.package) name version description;
|
inherit (cargoToml.package) name version description;
|
||||||
pname = name;
|
pname = name;
|
||||||
src = lib.cleanSourceWith {
|
src = cleanRustSrc ./.;
|
||||||
src = ./.;
|
|
||||||
name = "${pname}-source";
|
|
||||||
# Adapted from <https://github.com/ipetkov/crane/blob/master/lib/filterCargoSources.nix>
|
|
||||||
# no need to pull in crane for just this
|
|
||||||
filter =
|
|
||||||
orig_path: type:
|
|
||||||
let
|
|
||||||
path = toString orig_path;
|
|
||||||
base = baseNameOf path;
|
|
||||||
parentDir = baseNameOf (dirOf path);
|
|
||||||
matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [
|
|
||||||
# Rust sources
|
|
||||||
".rs"
|
|
||||||
# TOML files are often used to configure cargo based tools (e.g. .cargo/config.toml)
|
|
||||||
".toml"
|
|
||||||
];
|
|
||||||
isCargoLock = base == "Cargo.lock";
|
|
||||||
# .cargo/config.toml is captured above
|
|
||||||
isOldStyleCargoConfig = parentDir == ".cargo" && base == "config";
|
|
||||||
in
|
|
||||||
type == "directory" || matchesSuffix || isCargoLock || isOldStyleCargoConfig;
|
|
||||||
};
|
|
||||||
in
|
in
|
||||||
rustPlatform.buildRustPackage {
|
rustPlatform.buildRustPackage {
|
||||||
inherit pname version src;
|
inherit pname version src;
|
||||||
|
|
|
@ -1,37 +1,48 @@
|
||||||
{ lib, ... }:
|
{ lib, ... }:
|
||||||
let
|
let
|
||||||
src = ./.;
|
# Clean the package source leaving only the relevant rust files
|
||||||
# Autodetects files with a package.nix and calls `callPackage` on them.
|
cleanRustSrc =
|
||||||
#
|
pname: src:
|
||||||
# Will add a package .#dirname to the flake if it finds a ./dirname/package.nix file.
|
lib.cleanSourceWith {
|
||||||
files = builtins.readDir src;
|
inherit src;
|
||||||
isPackage = path: type: (type == "directory") && (builtins.readDir path) ? "package.nix";
|
name = "${pname}-source";
|
||||||
toPackage = name: pkgs: {
|
# Adapted from <https://github.com/ipetkov/crane/blob/master/lib/filterCargoSources.nix>
|
||||||
inherit name;
|
# no need to pull in crane for just this
|
||||||
value = pkgs.callPackage "${src}/${name}/package.nix" { };
|
filter =
|
||||||
};
|
orig_path: type:
|
||||||
# call pkgs.callPackage on all ./*/package.nix
|
|
||||||
makePackage =
|
|
||||||
pkgs: name:
|
|
||||||
let
|
let
|
||||||
type = files.${name};
|
path_str = toString orig_path;
|
||||||
path = "${src}/${name}";
|
base = baseNameOf path_str;
|
||||||
package = toPackage name pkgs;
|
parentDir = baseNameOf (dirOf path_str);
|
||||||
|
matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [
|
||||||
|
# Rust sources
|
||||||
|
".rs"
|
||||||
|
# TOML files are often used to configure cargo based tools (e.g. .cargo/config.toml)
|
||||||
|
".toml"
|
||||||
|
];
|
||||||
|
isCargoLock = base == "Cargo.lock";
|
||||||
|
# .cargo/config.toml is captured above
|
||||||
|
isOldStyleCargoConfig = parentDir == ".cargo" && base == "config";
|
||||||
in
|
in
|
||||||
# if it is a package then return a package otherwise return no package c:
|
type == "directory" || matchesSuffix || isCargoLock || isOldStyleCargoConfig;
|
||||||
if isPackage path type then [ package ] else [ ];
|
};
|
||||||
# we have lib.filterMapAttrs at home
|
# callPackage but for my rust Packages
|
||||||
scripts =
|
callRustPackage =
|
||||||
pkgs: builtins.listToAttrs (builtins.concatMap (makePackage pkgs) (builtins.attrNames files));
|
pkgs: pname: nixSrc:
|
||||||
|
pkgs.callPackage nixSrc { cleanRustSrc = cleanRustSrc pname; };
|
||||||
|
packages = {
|
||||||
|
jpassmenu = ./jpassmenu/package.nix;
|
||||||
|
audiomenu = ./audiomenu/package.nix;
|
||||||
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
# Add scripts to overlay
|
# Add scripts to overlay
|
||||||
flake.overlays.scripts = final: scripts;
|
flake.overlays.scripts = _final: prev: builtins.mapAttrs (callRustPackage prev);
|
||||||
|
|
||||||
# Add scripts to packages
|
# Add scripts to packages
|
||||||
perSystem =
|
perSystem =
|
||||||
{ pkgs, ... }:
|
{ pkgs, ... }:
|
||||||
{
|
{
|
||||||
packages = scripts pkgs;
|
packages = builtins.mapAttrs (callRustPackage pkgs);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,13 @@
|
||||||
{ lib, rustPlatform }:
|
{
|
||||||
|
lib,
|
||||||
|
rustPlatform,
|
||||||
|
cleanRustSrc,
|
||||||
|
}:
|
||||||
let
|
let
|
||||||
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
||||||
inherit (cargoToml.package) name version description;
|
inherit (cargoToml.package) name version description;
|
||||||
pname = name;
|
pname = name;
|
||||||
src = lib.cleanSourceWith {
|
src = cleanRustSrc ./.;
|
||||||
src = ./.;
|
|
||||||
name = "${pname}-source";
|
|
||||||
# Adapted from <https://github.com/ipetkov/crane/blob/master/lib/filterCargoSources.nix>
|
|
||||||
# no need to pull in crane for just this
|
|
||||||
filter =
|
|
||||||
orig_path: type:
|
|
||||||
let
|
|
||||||
path = toString orig_path;
|
|
||||||
base = baseNameOf path;
|
|
||||||
parentDir = baseNameOf (dirOf path);
|
|
||||||
matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [
|
|
||||||
# Rust sources
|
|
||||||
".rs"
|
|
||||||
# TOML files are often used to configure cargo based tools (e.g. .cargo/config.toml)
|
|
||||||
".toml"
|
|
||||||
];
|
|
||||||
isCargoLock = base == "Cargo.lock";
|
|
||||||
# .cargo/config.toml is captured above
|
|
||||||
isOldStyleCargoConfig = parentDir == ".cargo" && base == "config";
|
|
||||||
in
|
|
||||||
type == "directory" || matchesSuffix || isCargoLock || isOldStyleCargoConfig;
|
|
||||||
};
|
|
||||||
in
|
in
|
||||||
rustPlatform.buildRustPackage {
|
rustPlatform.buildRustPackage {
|
||||||
inherit pname version src;
|
inherit pname version src;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue