refactor: clean up scripts #485

Merged
jalil merged 1 commit from push-xqtoroovkvyz into main 2025-05-16 19:11:26 +02:00
3 changed files with 47 additions and 72 deletions
Showing only changes of commit dc5e219bc3 - Show all commits

View file

@ -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;

View file

@ -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:
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 in
{ {
# Add scripts to overlay # Add scripts to overlay
flake.overlays.scripts = final: scripts; flake.overlays.scripts = _final: prev: builtins.mapAttrs (callRustPackage prev) packages;
# Add scripts to packages # Add scripts to packages
perSystem = perSystem =
{ pkgs, ... }: { pkgs, ... }:
{ {
packages = scripts pkgs; packages = builtins.mapAttrs (callRustPackage pkgs) packages;
}; };
} }

View file

@ -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;