diff --git a/default.nix b/default.nix index 4c3ae64..ef7bd2a 100644 --- a/default.nix +++ b/default.nix @@ -1,3 +1,5 @@ +# MIT License, see below +# # These are some helpers for figuring out the derivations attributes of runtime # dependencies of a derivation, in particular the function `runtimeReport`. At # the bottom of the file you can see it used on `hello`. Spoiler: glibc is a @@ -8,7 +10,7 @@ # Let's call these "imports". They're functions used throughout the code. # Nothing interesting here. -{ program, system, pkgs ? import {}}: +{ drv, pkgs ? import {}}: with rec { inherit (pkgs) @@ -91,6 +93,7 @@ runtimeReport = drv: unique_by(.path) # Then we map over each build-time derivation and use `select()` # to keep only the ones that show up in $runtime + | map( # this little beauty checks if "obj.path" is in "runtime" select(. as $obj | $runtime | any(.[] | . == $obj.path)) | .report) @@ -189,4 +192,38 @@ cinfo = drv: runCommand "${drv.name}-cinfo" jq -c 'map(select( length > 0 ))' > $out ''; -in runtimeReport program +in { + runtimeReport = runtimeReport drv; + buildtimeDerivations = runCommand "${drv.name}-build" { + big = builtins.toJSON (buildtimeDerivations drv); + passAsFile = ["big"]; + buildInputs = [ pkgs.jq]; + } '' + cp $bigPath $out + ''; + # let result = buildtimeDerivations drv; + # in result; +} + +# MIT License +# +# Copyright (c) 2021 Nicolas Mattia +# +# 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.lock b/flake.lock index 495a9a8..c6957ba 100644 --- a/flake.lock +++ b/flake.lock @@ -1,6 +1,88 @@ { "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1623875721, + "narHash": "sha256-A8BU7bjS5GirpAUv4QA+QnJ4CceLHkcXdRp4xITDB0s=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "f7e004a55b120c02ecb6219596820fcd32ca8772", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nix-bundle": { + "inputs": { + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1626704917, + "narHash": "sha256-cJ73kxY5ZCPWQeQZyCSdvkBqjqJkU5wgETBNaXfqF18=", + "owner": "matthewbauer", + "repo": "nix-bundle", + "rev": "223f4ffc4179aa318c34dc873a08cb00090db829", + "type": "github" + }, + "original": { + "owner": "matthewbauer", + "repo": "nix-bundle", + "type": "github" + } + }, + "nix-utils": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1632973430, + "narHash": "sha256-9G8zo+0nfYAALV5umCyQR/2hVUFNH10JropBkyxZGGw=", + "owner": "juliosueiras-nix", + "repo": "nix-utils", + "rev": "b44e1ffd726aa03056db9df469efb497d8b9871b", + "type": "github" + }, + "original": { + "owner": "juliosueiras-nix", + "repo": "nix-utils", + "type": "github" + } + }, "nixpkgs": { + "locked": { + "lastModified": 1620055814, + "narHash": "sha256-8LEHoYSJiL901bTMVatq+rf8y7QtWuZhwwpKE2fyaRY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1db42b7fe3878f3f5f7a4f2dc210772fd080e205", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-20.03-small", + "type": "indirect" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1629252929, + "narHash": "sha256-Aj20gmGBs8TG7pyaQqgbsqAQ6cB+TVuL18Pk3DPBxcQ=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "3788c68def67ca7949e0864c27638d484389363d", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { "locked": { "lastModified": 1634782485, "narHash": "sha256-psfh4OQSokGXG0lpq3zKFbhOo3QfoeudRcaUnwMRkQo=", @@ -15,7 +97,9 @@ }, "root": { "inputs": { - "nixpkgs": "nixpkgs" + "nix-bundle": "nix-bundle", + "nix-utils": "nix-utils", + "nixpkgs": "nixpkgs_3" } } }, diff --git a/flake.nix b/flake.nix index de1b72b..dbbf4eb 100644 --- a/flake.nix +++ b/flake.nix @@ -1,29 +1,57 @@ { - description = "nix-generators"; + description = "Example bundlers"; inputs.nix-utils.url = "github:juliosueiras-nix/nix-utils"; - outputs = { self, nixpkgs, nix-utils }: { + inputs.nix-bundle.url = "github:matthewbauer/nix-bundle"; - defaultBundler = self.bundlers.toReport; + outputs = { self, nixpkgs, nix-bundle, nix-utils }: let + # System types to support. + supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; - bundlers = let - prog = program: with program; "${outPath}/bin/${if meta?mainProgram then meta.mainProgram else (builtins.parseDrvName name).name}"; - in { + # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; - toRPM = {program,system}: nix-utils.bundlers.rpm {inherit system; program=prog program;}; + # Nixpkgs instantiated for supported system types. + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); - toDEB = {program,system}: nix-utils.bundlers.deb {inherit system; program=prog program;}; + # Backwards compatibility helper for previous bundler API + program = p: with p; "${outPath}/bin/${ + if meta?mainProgram then + meta.mainProgram + else + (builtins.parseDrvName name).name + }"; + in { - toDockerImage = {program, system}: nixpkgs.legacyPackages.${system}.dockerTools.buildLayeredImage { - name = program.name; - tag = "latest"; - contents = [ program ]; + # defaultBundler.x86_64-linux = forAllSystems (system: self.bundlers.${system}.toArx); + defaultBundler.x86_64-linux = self.bundlers.x86_64-linux.toArx; + + bundlers = forAllSystems (system: { + toArx = drv: nix-bundle.bundlers.nix-bundle {inherit system; program=program drv;}; + + toRPM = drv: nix-utils.bundlers.rpm {inherit system; program=program drv;}; + + toDEB = drv: nix-utils.bundlers.deb {inherit system; program=program drv;}; + + toDockerImage = drv: + nixpkgs.legacyPackages.${system}.dockerTools.buildLayeredImage { + name = drv.name; + tag = "latest"; + contents = [ drv ]; }; - toReport = {program, system}: - import ./default.nix { - inherit program system; - pkgs = nixpkgs.legacyPackages.${system};}; - }; - }; + toBuildDerivation = drv: + (import ./default.nix { + inherit drv; + pkgs = nixpkgsFor.${system};}).buildtimeDerivations; + + toReport = drv: + builtins.trace drv + (import ./default.nix { + inherit drv; + pkgs = nixpkgsFor.${system};}).runtimeReport; + + identity = drv: drv; + }); + }; }