-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkgs.dockertools.buildLayeredImage: customisable layering strategy #122608
Merged
tomberek
merged 7 commits into
NixOS:master
from
adrian-gierakowski:ag/docker-customisable-layering-strategy
Nov 30, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b4a8db
build-support/docker: customisable layering strategy
adrian-gierakowski 6cecfd8
docker-tool: fix maxLayers assert
adrian-gierakowski f621753
flatten_references_graph: remove unused fixture and all real /nix/sto…
adrian-gierakowski 88bdfb1
flatten_references_graph: fix typo egdes => edges
adrian-gierakowski 266283b
build-support/docker: use runCommand in make-layers.nix
adrian-gierakowski 9ae0f77
flattenReferencesGraph: use lib.fileset instead of gitignoreSource
adrian-gierakowski ba5a5fa
flattenReferencesGraph: fix use of lib.fileset
adrian-gierakowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
coreutils, | ||
flattenReferencesGraph, | ||
lib, | ||
jq, | ||
runCommand, | ||
}: | ||
{ | ||
closureRoots, | ||
excludePaths ? [ ], | ||
# This could be a path to (or a derivation producing a path to) | ||
# a json file containing the pipeline | ||
pipeline ? [ ], | ||
debug ? false, | ||
}: | ||
if closureRoots == [ ] then | ||
builtins.toFile "docker-layers-empty" "[]" | ||
else | ||
runCommand "docker-layers" | ||
{ | ||
__structuredAttrs = true; | ||
# graph, exclude_paths and pipeline are expected by the | ||
# flatten_references_graph executable. | ||
exportReferencesGraph.graph = closureRoots; | ||
exclude_paths = excludePaths; | ||
inherit pipeline; | ||
nativeBuildInputs = [ | ||
coreutils | ||
flattenReferencesGraph | ||
jq | ||
]; | ||
} | ||
'' | ||
. .attrs.sh | ||
|
||
flatten_references_graph_arg=.attrs.json | ||
|
||
echo "pipeline: $pipeline" | ||
|
||
if jq -e '.pipeline | type == "string"' .attrs.json; then | ||
jq '. + { "pipeline": $pipeline[0] }' \ | ||
--slurpfile pipeline "$pipeline" \ | ||
.attrs.json > flatten_references_graph_arg.json | ||
|
||
flatten_references_graph_arg=flatten_references_graph_arg.json | ||
fi | ||
|
||
${lib.optionalString debug "export DEBUG=True"} | ||
flatten_references_graph "$flatten_references_graph_arg" > ''${outputs[out]} | ||
'' |
34 changes: 34 additions & 0 deletions
34
pkgs/build-support/docker/popularity-contest-layering-pipeline.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
lib, | ||
runCommand, | ||
jq, | ||
}: | ||
{ | ||
maxLayers, | ||
fromImage ? null, | ||
}: | ||
runCommand "popularity-contest-layering-pipeline.json" { inherit maxLayers; } '' | ||
# Compute the number of layers that are already used by a potential | ||
# 'fromImage' as well as the customization layer. Ensure that there is | ||
# still at least one layer available to store the image contents. | ||
# one layer will be taken up by the customisation layer | ||
usedLayers=1 | ||
|
||
${lib.optionalString (fromImage != null) '' | ||
# subtract number of base image layers | ||
baseImageLayersCount=$(tar -xOf "${fromImage}" manifest.json | ${lib.getExe jq} '.[0].Layers | length') | ||
|
||
(( usedLayers += baseImageLayersCount )) | ||
''} | ||
|
||
if ! (( $usedLayers < $maxLayers )); then | ||
echo >&2 "Error: usedLayers $usedLayers layers to store 'fromImage' and" \ | ||
"'extraCommands', but only maxLayers=$maxLayers were" \ | ||
"allowed. At least 1 layer is required to store contents." | ||
exit 1 | ||
fi | ||
availableLayers=$(( maxLayers - usedLayers )) | ||
|
||
# Produce pipeline which uses popularity_contest algo. | ||
echo '[["popularity_contest"],["limit_layers",'$availableLayers']]' > $out | ||
'' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Start this shell with: | ||
# nix-shell path/to/root/of/nixpkgs -A flattenReferencesGraph.dev-shell | ||
{ | ||
mkShell, | ||
callPackage, | ||
python3Packages, | ||
}: | ||
let | ||
helpers = callPackage (import ./helpers.nix) { }; | ||
in | ||
mkShell { | ||
inputsFrom = [ (callPackage (import ./package.nix) { }) ]; | ||
buildInputs = [ | ||
helpers.format | ||
helpers.lint | ||
helpers.unittest | ||
# This is needed to plot graphs when DEBUG_PLOT is set to True. | ||
python3Packages.pycairo | ||
# This can be used on linux to display the graphs. | ||
# On other platforms the image viewer needs to be set with | ||
# DEBUG_PLOT_IMAGE_VIEWER env var. | ||
# pkgs.gwenview | ||
]; | ||
shellHook = '' | ||
echo ' | ||
********************************************************************** | ||
********************************************************************** | ||
|
||
Commands useful for development (should be executed from scr dir): | ||
|
||
|
||
format | ||
* formats all files in place using autopep8 | ||
|
||
lint | ||
* lints all files using flake8 | ||
|
||
unittest | ||
* runs all unit tests | ||
|
||
following env vars can be set to enable extra output in tests: | ||
- DEBUG=True - enable debug logging | ||
- DEBUG_PLOT=True - plot graphs processed by split_paths.py and | ||
subcomponent.py | ||
- DEBUG_PLOT_IMAGE_VIEWER=$PATH_OF_IMAGE_VIEWER_APP - app used to | ||
display plots (default: gwenview) | ||
- DEBUG_PLOT_SAVE_BASE_NAME=$SOME_NAME - if set, plots will be saved | ||
to files instead of displayed with image viewer | ||
|
||
********************************************************************** | ||
********************************************************************** | ||
' | ||
''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
bash, | ||
writers, | ||
python3Packages, | ||
}: | ||
let | ||
writeCheckedBashBin = | ||
name: | ||
let | ||
interpreter = "${bash}/bin/bash"; | ||
in | ||
writers.makeScriptWriter { | ||
inherit interpreter; | ||
check = "${interpreter} -n $1"; | ||
} "/bin/${name}"; | ||
|
||
# Helpers used during build/development. | ||
lint = writeCheckedBashBin "lint" '' | ||
${python3Packages.flake8}/bin/flake8 --show-source ''${@} | ||
''; | ||
|
||
unittest = writeCheckedBashBin "unittest" '' | ||
if [ "$#" -eq 0 ]; then | ||
set -- discover -p '*_test.py' | ||
fi | ||
|
||
${python3Packages.python}/bin/python -m unittest "''${@}" | ||
''; | ||
|
||
format = writeCheckedBashBin "format" '' | ||
${python3Packages.autopep8}/bin/autopep8 -r -i . "''${@}" | ||
''; | ||
in | ||
{ | ||
inherit format lint unittest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
callPackage, | ||
lib, | ||
python3Packages, | ||
}: | ||
let | ||
inherit (lib) fileset; | ||
helpers = callPackage ./helpers.nix { }; | ||
pythonPackages = python3Packages; | ||
in | ||
pythonPackages.buildPythonApplication { | ||
version = "0.1.0"; | ||
pname = "flatten-references-graph"; | ||
|
||
src = fileset.toSource { | ||
root = ./src; | ||
fileset = fileset.unions [ | ||
./src/.flake8 | ||
./src/flatten_references_graph | ||
./src/setup.py | ||
]; | ||
}; | ||
|
||
propagatedBuildInputs = with pythonPackages; [ | ||
igraph | ||
toolz | ||
]; | ||
|
||
doCheck = true; | ||
|
||
checkPhase = '' | ||
${helpers.unittest}/bin/unittest | ||
''; | ||
|
||
passthru = { | ||
dev-shell = callPackage ./dev-shell.nix { }; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[flake8] | ||
max-line-length = 80 | ||
[pep8] | ||
aggressive = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for people who would like to experiment with different ways of grouping paths into layers, this could now be override (at pkgs level) in order to inject arbitrary code for generating layers
probably not the most friendly interface but at least it's an improvement over status quo of
streamLayeredImage
being tightly coupled to referencesByPopularity approachI'd be happy to iterate on this in order to make the injection of layering logic more user friendly.