From 63c01758b52661cedaeb9de0ba39559117578aa8 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 28 Jul 2024 20:36:42 -0700 Subject: [PATCH] Split out MIT code --- closure.nix | 86 ++++++++++++++++++++++++++++++++++++++++++ flake.nix | 2 +- default.nix => lib.nix | 40 +------------------- 3 files changed, 88 insertions(+), 40 deletions(-) create mode 100644 closure.nix rename default.nix => lib.nix (66%) diff --git a/closure.nix b/closure.nix new file mode 100644 index 0000000..7adf6cd --- /dev/null +++ b/closure.nix @@ -0,0 +1,86 @@ +# MIT license, see below +# +# These are helper functions for determining buildtime closures. +# Sources: +# - https://github.com/NixOS/bundlers/blob/00762a03a3d862a2ca6272a21fdc50bda5d36c42/report/default.nix +# - https://nmattia.com/posts/2019-10-08-runtime-dependencies.html + +{ pkgs, ... }: +let + inherit (builtins) + genericClosure + hasAttr + map + ; + inherit (pkgs.lib) + concatLists + concatMap + filter + isDerivation + isList + mapAttrsToList + ; + + # Map a drv or list of drvs to a list of output drvs + drvOutputs = drv: + if isList drv then + concatMap drvOutputs drv + else if hasAttr "outputs" drv then + map (out: drv.${out}) drv.outputs + else [ drv ]; + + # Map a drv or list of drvs to the outputs of referenced derivations + drvDeps = + drv: + if isList drv then + concatMap drvDeps drv + else + mapAttrsToList ( + k: v: + if isDerivation v then + (drvOutputs v) + else if isList v then + concatMap drvOutputs (filter isDerivation v) + else + [ ] + ) drv; +in +{ + # Get the reference closure of a derivation or list of derivations + # This may miss dependencies that are only in-closure from string context + drvClosure = + let + wrap = drv: { + key = drv.outPath; + inherit drv; + }; + in + drv: + map (obj: obj.drv) (genericClosure { + startSet = map wrap (drvOutputs drv); + operator = obj: map wrap (concatLists (drvDeps obj.drv.drvAttrs)); + }); +} + +# MIT License +# +# Copyright (c) 2021 Nicolas Mattia, Tim Van Baak +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# diff --git a/flake.nix b/flake.nix index bed8963..581b8ad 100644 --- a/flake.nix +++ b/flake.nix @@ -4,7 +4,7 @@ let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; - lib = import ./default.nix { inherit pkgs; }; + lib = import ./lib.nix { inherit pkgs; }; inherit (lib) allMaintainers expandInput diff --git a/default.nix b/lib.nix similarity index 66% rename from default.nix rename to lib.nix index 8ebf9c8..1cdcc03 100644 --- a/default.nix +++ b/lib.nix @@ -20,47 +20,9 @@ let isList mapAttrsToList ; + inherit (import ./closure.nix { inherit pkgs; }) drvClosure; in rec { - # Map a drv or list of drvs to a list of output drvs - drvOutputs = drv: - if isList drv then - concatMap drvOutputs drv - else if hasAttr "outputs" drv then - map (out: drv.${out}) drv.outputs - else [ drv ]; - - # Map a drv or list of drvs to the outputs of referenced derivations - drvDeps = - drv: - if isList drv then - concatMap drvDeps drv - else - mapAttrsToList ( - k: v: - if isDerivation v then - (drvOutputs v) - else if isList v then - concatMap drvOutputs (filter isDerivation v) - else - [ ] - ) drv; - - # Get the reference closure of a derivation or list of derivations - # This may miss dependencies that are only in-closure from string context - drvClosure = - let - wrap = drv: { - key = drv.outPath; - inherit drv; - }; - in - drv: - map (obj: obj.drv) (genericClosure { - startSet = map wrap (drvOutputs drv); - operator = obj: map wrap (concatLists (drvDeps obj.drv.drvAttrs)); - }); - maintainable = drv: hasAttr "meta" drv && hasAttr "maintainers" drv.meta; drvMaintainers = drv: if maintainable drv then drv.meta.maintainers else [ ];