-
Notifications
You must be signed in to change notification settings - Fork 0
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
Build Docker images with Nix #36
Draft
btlogy
wants to merge
21
commits into
main
Choose a base branch
from
35.nix-build
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3c9bc56
Build Docker images with Nix
btlogy b68716c
Try better tag based on version
btlogy 2867d33
Try to scan the vulnerabilities
btlogy b20aa90
Ignore dup dep from dev
btlogy 47a8fe7
Fix grep expr
btlogy 5c44ae3
Remove unwanted spaces
btlogy fee2bae
Use a lib and sub-packages
btlogy 25b219d
Ignore nix build results
btlogy 4fc323b
Fix image name
btlogy ed8f9ef
Use matrix build and improve scan
btlogy 2cf882d
Fix workflow syntax
btlogy e9db7d5
Fix workflow again
btlogy a837243
Fix interpolation
btlogy f7be978
Move and fix the nix helper
btlogy 263f076
Run local image
btlogy c498a36
Do not fail on scan result
btlogy c243e00
Rename workflows and avoid useless build
btlogy 4def9cf
Cover relevant all files as today
btlogy 3b1e15e
Fix build filter
btlogy 788675e
Update comment and drop unused nixRev
btlogy d0fe09a
Use environment variable for store first
btlogy 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,67 @@ | ||
--- | ||
name: NixOS Images | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- '.github/workflows/docker-nix.yml' | ||
- 'nixpkgs.json' | ||
- '*.nix' | ||
- '*/*.nix' | ||
- 'nix/**' | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- '.github/workflows/docker-nix.yml' | ||
- 'nixpkgs.json' | ||
- '*.nix' | ||
- '*/*.nix' | ||
- 'nix/**' | ||
|
||
jobs: | ||
build: | ||
name: Build images | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
target: | ||
- directory: mailbox | ||
image: leastauthority/magic-wormhole-mailbox | ||
- directory: relay | ||
image: leastauthority/magic-wormhole-relay | ||
- directory: wormhole | ||
image: leastauthority/magic-wormhole | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install Nix | ||
uses: cachix/install-nix-action@v20 | ||
with: | ||
nix_path: nixpkgs=channel:nixos-22.11 | ||
|
||
- name: Build image with Nix | ||
run: | | ||
docker load --input $(nix-build ${{ matrix.target.directory }}) | ||
|
||
- name: Get image tag | ||
run: echo "tag=$(docker images "${{ matrix.target.image }}" --format "{{.Tag}}")" >> $GITHUB_ENV | ||
|
||
- name: Extract Python requirements | ||
run: | | ||
docker run --rm -t \ | ||
--entrypoint python \ | ||
-v $(pwd)/nix/helpers:/helpers \ | ||
${{ matrix.target.image }}:${{ env.tag }} /helpers/requirements-from-nix.py \ | ||
| grep -vE '==[0-9]+(\.[0-9]+){0,2}-dev' \ | ||
> requirements.txt | ||
# The grep above avoids dupplicated requirements from dev!? | ||
|
||
- name: Scan Python requirements | ||
uses: pypa/gh-action-pip-audit@v1.0.0 | ||
with: | ||
inputs: requirements.txt | ||
continue-on-error: true # TODO: comment on PR and run weekly |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/.idea | ||
result* |
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,7 @@ | ||
{ pkgs ? import ./nixpkgs.nix { } }: | ||
{ | ||
# Build our Docker images | ||
mailbox = pkgs.callPackage ./mailbox { }; | ||
relay = pkgs.callPackage ./relay { }; | ||
wormhole = pkgs.callPackage ./wormhole { }; | ||
} |
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,19 @@ | ||
{ pkgs ? import ../nixpkgs.nix { } }: | ||
let | ||
# Call our lib to build the image | ||
output = pkgs.callPackage ../nix/lib/docker-image.nix { | ||
pname = "magic-wormhole-mailbox-server"; | ||
iname = "leastauthority/magic-wormhole-mailbox"; | ||
config = { | ||
WorkingDir = "/app"; | ||
Volumes = { "/db" = { }; }; | ||
Cmd = [ | ||
"twist" | ||
"wormhole-mailbox" | ||
"--usage-db=/db/usage-relay.sqlite" | ||
"--blur-usage=3600" | ||
"--channel-db=/db/relay.sqlite" | ||
]; | ||
}; | ||
}; | ||
in output |
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,20 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import re | ||
|
||
# Pattern for Python modules | ||
p = re.compile('python[0-9]+\.[0-9]+-(.+)-([0-9]+(\.[0-9]+){0,2})(-.+)?') | ||
|
||
# Iterate through Nix store | ||
for dir in os.listdir(os.getenv("NIX_STORE_DIR","/nix/store")): | ||
# Remove the fix hash part | ||
pkg = dir[33:] | ||
# Match Python modules | ||
m = p.match(pkg) | ||
# Print as requirement | ||
if m: | ||
name = m.group(1) | ||
version = m.group(2) | ||
suffix = m.group(4) or "" | ||
print('{}=={}{}'.format(name, version, suffix)) |
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,19 @@ | ||
{ dockerTools, python3, lib, pname, iname ? pname, config ? {} }: | ||
let | ||
# Inject mailbox in the standard Python env | ||
pyenv = python3.buildEnv.override { | ||
extraLibs = [ python3.pkgs.${pname} ]; | ||
ignoreCollisions = true; | ||
}; | ||
# Parse Python package and NixOS versions to tag the image | ||
pkgVersion = python3.pkgs.${pname}.version; | ||
libVersion = lib.lists.flatten (lib.lists.sublist 1 1 (builtins.split "([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)" lib.version)); | ||
nixVersion = builtins.concatStringsSep "." (lib.lists.take 3 libVersion); | ||
in | ||
# Build the image with our custom CMD | ||
dockerTools.buildLayeredImage { | ||
name = iname; | ||
tag = "${pkgVersion}-nixos-${nixVersion}"; | ||
config = config; | ||
contents = [ pyenv ]; | ||
} |
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,5 @@ | ||
{ | ||
"name": "source", | ||
"url": "https://releases.nixos.org/nixos/21.11/nixos-21.11.337975.eabc3821918/nixexprs.tar.xz", | ||
"sha256": "1fq3zz7qfavksdbqvicns7hg61q3hhbxs2ibm818gy629hwkvsvm" | ||
} |
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,2 @@ | ||
# Import local nixpkg.json which pins all our Nix packages | ||
import (builtins.fetchTarball (builtins.fromJSON (builtins.readFile ./nixpkgs.json))) | ||
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,19 @@ | ||
{ pkgs ? import ../nixpkgs.nix { } }: | ||
let | ||
output = pkgs.callPackage ../nix/lib/docker-image.nix { | ||
pname = "magic-wormhole-transit-relay"; | ||
iname = "leastauthority/magic-wormhole-relay"; | ||
config = { | ||
WorkingDir = "/app"; | ||
Volumes = { "/db" = { }; }; | ||
Cmd = [ | ||
"twist" | ||
"transitrelay" | ||
"--usage-db=/db/usage-transitrelay.sqlite" | ||
"--blur-usage=3600" | ||
"--port=tcp:4001" | ||
"--websocket=tcp:4002" | ||
]; | ||
}; | ||
}; | ||
in output |
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,14 @@ | ||
{ pkgs ? import ../nixpkgs.nix { } }: | ||
let | ||
# Call our lib to build the image | ||
output = pkgs.callPackage ../nix/lib/docker-image.nix { | ||
pname = "magic-wormhole"; | ||
iname = "leastauthority/magic-wormhole"; | ||
config = { | ||
WorkingDir = "/app"; | ||
EntryPoint = [ | ||
"wormhole" | ||
]; | ||
}; | ||
}; | ||
in output |
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.
Might want to consider using a tool like npins or niv here, it will also make updating the pin much more pleasant
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.
I was looking into niv, but I could not see how/if it is used for PrivateStorageio!
(I've just made a copy/paste :-)
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.
@exarkun : how do we update nixpkgs in PrivateStorageio?
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.
The shell script
ci-tools/update-nixpkgs
calls, in its functionupdate_nixpkgs
on line 160,update-nixpkgs
defined intools/default.nix
line 17 viashell.nix
line 6. That in turn calls the python scripttools/update-nixpkgs
to updatenixpkgs.json
.The python script already broke once because the HTTP library it uses changed its API and we didn't have its version pinned.
That's why the moment @exarkun turns his back I'll replace the whole thing by my 12 line
update-nixpkgs.sh
shell script I have still sitting in my working directory which just creates a new JSON from a heredoc hr hr hrThere 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.
Thank you @hacklschorsch.
I was surprised there is not some nix command to do this...
And I was already trying to implement this in bash. :-)
I guess I'll not go further here.
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.
FWIW I think there are nix commands built-in when using flakes …
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.
I did not dare pushing this experiment to the flake level :-)
Nice to know this could help to simplify the update of the nixpkgs.
I'll see later if I can spend some extra time to improve this (with
npins
,niv
orflake
).Maybe interesting case for a "Nix Intro" session?
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.
I won't be there this week because national holiday, but next week I could talk a bit about these
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.
No business value in adding yet another way to update one three line json file.