Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
Nix
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100{ lib, pkgs, root, src,}: let fs = lib.fileset; manifest = lib.importTOML (root + "/Cargo.toml"); workspaceDeps = (manifest.workspace or {}).dependencies or {};
depSections = ["dependencies" "build-dependencies" "dev-dependencies"];
deps = man: lib.concatLists (map (section: lib.mapAttrsToList (name: spec: {inherit name spec;}) (lib.filterAttrs (_: lib.isAttrs) (man.${section} or {}))) depSections);
normalise = path: lib.concatStringsSep "/" (lib.foldl' (segments: segment: if segment == ".." then lib.init segments else if lib.elem segment ["." ""] then segments else segments ++ [segment]) [] (lib.splitString "/" path));
resolve = dir: { name, spec, }: if spec ? path then normalise "${dir}/${spec.path}" else if spec.workspace or false then let ws = workspaceDeps.${name} or {}; in if ws ? path then normalise ws.path else null else null;
pathDeps = dir: let man = lib.importTOML (root + "/${dir}/Cargo.toml"); specs = deps man ++ lib.concatLists (map deps (lib.attrValues (man.target or {}))); in lib.filter (dep: dep != null) (map (resolve dir) specs);
closure = crate: let grow = dirs: lib.unique (lib.concatMap (dir: [dir] ++ pathDeps dir) dirs); fix = dirs: let next = grow dirs; in if next == dirs then dirs else fix next; in fix [crate];
patchPaths = lib.filter (dir: dir != null) (map (spec: if lib.isAttrs spec && spec ? path then normalise spec.path else null) (lib.concatLists (lib.mapAttrsToList (_: specs: lib.attrValues specs) (manifest.patch or {}))));
narrow = members: let lines = lib.splitString "\n" (builtins.readFile (root + "/Cargo.toml")); has = key: lib.any (line: lib.hasPrefix "${key} =" line) lines; missing = lib.filter (key: !(has key)) ["members" "default-members"]; replace = line: if lib.hasPrefix "members =" line then "members = ${builtins.toJSON members}" else if lib.hasPrefix "default-members =" line then "default-members = ${builtins.toJSON members}" else line; in if missing == [] then lib.concatStringsSep "\n" (map replace lines) else throw "nix/rust-src.nix: Cargo.toml has no single-line ${lib.concatStringsSep " or " missing} key to narrow";
mkRustSrc = crate: let members = closure crate; files = ["Cargo.toml" "Cargo.lock" "lexicons"] ++ members ++ patchPaths; filtered = fs.toSource { root = root; fileset = fs.intersection (fs.fromSource src) (fs.unions (map (file: root + "/${file}") files)); }; in pkgs.runCommandLocal "rust-src-${lib.replaceStrings ["/"] ["-"] crate}" {} '' mkdir -p $out cp -r --no-preserve=mode,ownership ${filtered}/. $out/ cp ${pkgs.writeText "Cargo.toml" (narrow members)} $out/Cargo.toml '';in mkRustSrc