Skip to content

Commit d938809

Browse files
Pull images directly into tools definition
* add a docker_rag function to extract best practices * switch the javascript-runner to be non-nix based because I'm not happy with the node2nix translation layer and I needed uuid and node-csv pulled in to node_modules * fix write_files to support relative paths * create single file versions of curl, dockerfiles, and project_type * support an `image` reference in tools
1 parent 680b4ee commit d938809

File tree

26 files changed

+1202
-99
lines changed

26 files changed

+1202
-99
lines changed

functions/docker_rag/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# syntax = docker/dockerfile:1.4
3+
FROM nixos/nix:2.21.1@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder
4+
5+
WORKDIR /tmp/build
6+
RUN mkdir /tmp/nix-store-closure
7+
8+
# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
9+
# hadolint ignore=SC2046
10+
RUN --mount=type=cache,target=/nix,from=nixos/nix:2.21.1,source=/nix \
11+
--mount=type=cache,target=/root/.cache \
12+
--mount=type=bind,target=/tmp/build \
13+
<<EOF
14+
nix \
15+
--extra-experimental-features "nix-command flakes" \
16+
--option filter-syscalls false \
17+
--extra-trusted-substituters "https://cache.iog.io" \
18+
--extra-trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" \
19+
--show-trace \
20+
--log-format raw \
21+
build . --out-link /tmp/output/result
22+
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
23+
EOF
24+
25+
FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7
26+
27+
WORKDIR /app
28+
29+
COPY --from=builder /tmp/nix-store-closure /nix/store
30+
COPY --from=builder /tmp/output/ /app/
31+
32+
ENTRYPOINT ["/app/result/bin/entrypoint"]
33+
CMD ["--help"]

functions/docker_rag/flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/docker_rag/flake.nix

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
description = "{{tool}}";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils, ...}@inputs:
10+
11+
flake-utils.lib.eachDefaultSystem
12+
(system:
13+
let
14+
pkgs = import nixpkgs {
15+
inherit system;
16+
};
17+
18+
in rec
19+
{
20+
packages = rec {
21+
22+
# this derivation just contains the init.clj script
23+
scripts = pkgs.stdenv.mkDerivation {
24+
name = "scripts";
25+
src = ./.;
26+
installPhase = ''
27+
mkdir -p $out
28+
cp init.clj $out
29+
cp npm-best-practices.md $out
30+
'';
31+
};
32+
33+
run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
34+
cd ${scripts}
35+
/usr/local/bin/bb ${scripts}/init.clj "$@"
36+
'';
37+
38+
default = pkgs.buildEnv {
39+
name = "bb";
40+
paths = [ run-entrypoint ];
41+
};
42+
};
43+
});
44+
}
45+

functions/docker_rag/init.clj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(ns init
2+
(:require
3+
[cheshire.core]))
4+
5+
(try
6+
(let [[json-string & extra-args] *command-line-args*
7+
m (cheshire.core/parse-string json-string true)
8+
script (first extra-args)]
9+
(println
10+
(slurp "npm-best-practices.md")))
11+
(catch Throwable t
12+
(binding [*out* *err*]
13+
(println (str "Error: " (.getMessage t)))
14+
(System/exit 1))))
15+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Write Dockerfiles for NPM projects using three stages. Do these three steps sequentially.
2+
* the first node depemdencies stage should be called "deps"
3+
and it should fetch the runtime dependencies using npm ci
4+
`with the --omit=dev` flag.
5+
* The second Dockerfile stage should be called build
6+
and it should be based on the deps stage.
7+
It should run npm ci and then npm build
8+
* The third Dockerfile stage should select a base image
9+
that is recommended by Scout and use that in the FROM line.
10+
This stage should do then do three things.
11+
1. it copies the node_modules directory from the deps stage.
12+
2. it copies the dist directory from the build stage.
13+
3. it then runs npm start
14+
15+
If you need to use a RUN statement containing `npm ci` always
16+
add the argument `--mount=type=cache,target=/root/.npm` to the RUN instruction.
17+
The `--mount` argument should be placed between the word RUN and the npm command.
18+
This will cache the npm packages in the docker build cache and speed up the build process.
19+

functions/docker_rag/runbook.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
```sh
3+
docker build -t vonwig/docker-rag:latest .
4+
```
5+
6+
```sh
7+
docker run vonwig/docker-rag:latest '{}'
8+
```
9+
10+
```sh
11+
docker push vonwig/docker-rag:latest
12+
```
Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
1-
# syntax = docker/dockerfile:1.4
2-
FROM nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder
3-
4-
WORKDIR /tmp/build
5-
RUN mkdir /tmp/nix-store-closure
6-
7-
# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
8-
# hadolint ignore=SC2046
9-
RUN --mount=type=cache,target=/nix,from=nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9,source=/nix \
10-
--mount=type=cache,target=/root/.cache \
11-
--mount=type=bind,target=/tmp/build \
12-
<<EOF
13-
nix \
14-
--extra-experimental-features "nix-command flakes" \
15-
--option filter-syscalls false \
16-
--show-trace \
17-
--log-format raw \
18-
build . --out-link /tmp/output/result
19-
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
20-
EOF
21-
22-
FROM scratch
23-
1+
# The first stage, node dependencies stage - "deps"
2+
FROM node:22-slim@sha256:377674fd5bb6fc2a5a1ec4e0462c4bfd4cee1c51f705bbf4bda0ec2c9a73af72 AS deps
243
WORKDIR /app
4+
COPY package*.json ./
5+
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
256

26-
COPY --from=builder /tmp/nix-store-closure /nix/store
27-
COPY --from=builder /tmp/output/ /app/
7+
# The second stage - "build"
8+
FROM deps AS build
9+
RUN --mount=type=cache,target=/root/.npm npm ci
2810

29-
ENTRYPOINT ["/app/result/bin/entrypoint"]
11+
# The third stage, selecting a base image recommended by Scout
12+
FROM node:22-slim@sha256:377674fd5bb6fc2a5a1ec4e0462c4bfd4cee1c51f705bbf4bda0ec2c9a73af72
13+
WORKDIR /app
14+
COPY --from=deps /app/node_modules ./node_modules
15+
COPY ./main.js ./main.js
16+
ENTRYPOINT ["node", "/app/main.js"]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# syntax = docker/dockerfile:1.4
2+
FROM nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder
3+
4+
WORKDIR /tmp/build
5+
RUN mkdir /tmp/nix-store-closure
6+
7+
# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
8+
# hadolint ignore=SC2046
9+
RUN --mount=type=cache,target=/nix,from=nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9,source=/nix \
10+
--mount=type=cache,target=/root/.cache \
11+
--mount=type=bind,target=/tmp/build \
12+
<<EOF
13+
nix \
14+
--extra-experimental-features "nix-command flakes" \
15+
--option filter-syscalls false \
16+
--show-trace \
17+
--log-format raw \
18+
build . --out-link /tmp/output/result
19+
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
20+
EOF
21+
22+
FROM scratch
23+
24+
WORKDIR /app
25+
26+
COPY --from=builder /tmp/nix-store-closure /nix/store
27+
COPY --from=builder /tmp/output/ /app/
28+
29+
ENTRYPOINT ["/app/result/bin/entrypoint"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file has been generated by node2nix 1.11.1. Do not edit!
2+
3+
{pkgs ? import <nixpkgs> {
4+
inherit system;
5+
}, system ? builtins.currentSystem, nodejs ? pkgs."nodejs_14"}:
6+
7+
let
8+
nodeEnv = import ./node-env.nix {
9+
inherit (pkgs) stdenv lib python2 runCommand writeTextFile writeShellScript;
10+
inherit pkgs nodejs;
11+
libtool = if pkgs.stdenv.isDarwin then pkgs.cctools or pkgs.darwin.cctools else null;
12+
};
13+
in
14+
import ./node-packages.nix {
15+
inherit (pkgs) fetchurl nix-gitignore stdenv lib fetchgit;
16+
inherit nodeEnv;
17+
}

functions/javascript-runner/flake.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
packages = rec {
2929

30+
nodeDependencies = (pkgs.callPackage ./default.nix {}).nodeDependencies;
31+
3032
# this derivation just contains the init.clj script
3133
scripts = pkgs.stdenv.mkDerivation {
3234
name = "scripts";

0 commit comments

Comments
 (0)