refactor: clean up scripts
Some checks failed
/ check (push) Successful in 8s
/ check-renovaterc (push) Successful in 3s
/ build-packages (push) Failing after 1s
/ build-vm (push) Has been skipped
/ report-size (push) Has been skipped

Tidy up the script logic and deduplicate it a bit.
This commit is contained in:
Jalil David Salamé Messina 2025-05-16 19:03:09 +02:00
parent 3977e8d50e
commit d4997ba0c1
Signed by: jalil
GPG key ID: F016B9E770737A0B
3 changed files with 47 additions and 72 deletions

View file

@ -1,31 +1,13 @@
{ lib, rustPlatform }:
{
lib,
rustPlatform,
cleanRustSrc,
}:
let
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
inherit (cargoToml.package) name version description;
pname = name;
src = lib.cleanSourceWith {
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;
};
src = cleanRustSrc ./.;
in
rustPlatform.buildRustPackage {
inherit pname version src;

View file

@ -1,37 +1,48 @@
{ lib, ... }:
let
src = ./.;
# Autodetects files with a package.nix and calls `callPackage` on them.
#
# Will add a package .#dirname to the flake if it finds a ./dirname/package.nix file.
files = builtins.readDir src;
isPackage = path: type: (type == "directory") && (builtins.readDir path) ? "package.nix";
toPackage = name: pkgs: {
inherit name;
value = pkgs.callPackage "${src}/${name}/package.nix" { };
# Clean the package source leaving only the relevant rust files
cleanRustSrc =
pname: src:
lib.cleanSourceWith {
inherit 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_str = toString orig_path;
base = baseNameOf path_str;
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
type == "directory" || matchesSuffix || isCargoLock || isOldStyleCargoConfig;
};
# callPackage but for my rust Packages
callRustPackage =
pkgs: pname: nixSrc:
pkgs.callPackage nixSrc { cleanRustSrc = cleanRustSrc pname; };
packages = {
jpassmenu = ./jpassmenu/package.nix;
audiomenu = ./audiomenu/package.nix;
};
# call pkgs.callPackage on all ./*/package.nix
makePackage =
pkgs: name:
let
type = files.${name};
path = "${src}/${name}";
package = toPackage name pkgs;
in
# if it is a package then return a package otherwise return no package c:
if isPackage path type then [ package ] else [ ];
# we have lib.filterMapAttrs at home
scripts =
pkgs: builtins.listToAttrs (builtins.concatMap (makePackage pkgs) (builtins.attrNames files));
in
{
# Add scripts to overlay
flake.overlays.scripts = final: scripts;
flake.overlays.scripts = _final: prev: builtins.mapAttrs (callRustPackage prev);
# Add scripts to packages
perSystem =
{ pkgs, ... }:
{
packages = scripts pkgs;
packages = builtins.mapAttrs (callRustPackage pkgs);
};
}

View file

@ -1,31 +1,13 @@
{ lib, rustPlatform }:
{
lib,
rustPlatform,
cleanRustSrc,
}:
let
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
inherit (cargoToml.package) name version description;
pname = name;
src = lib.cleanSourceWith {
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;
};
src = cleanRustSrc ./.;
in
rustPlatform.buildRustPackage {
inherit pname version src;