Skip to content

Commit 76b41dc

Browse files
Add a function to extract your host ip
1 parent 7bc4a40 commit 76b41dc

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

functions/what_is_my_ip/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

functions/what_is_my_ip/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 scratch
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"]

functions/what_is_my_ip/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Background
2+
3+
This tool is based on a [post][learn-nix]. It's a good proven way to extract a host ip from a
4+
docker container (that is using host networking).
5+
6+
### Function
7+
8+
Add this function definition to any set of prompts that might need to extract the host IP address.
9+
10+
```yaml
11+
- container:
12+
image: vonwig/what-is-my-ip:latest
13+
name: what-is-my-ip
14+
description: Get the host IP address for this machine.
15+
```
16+
17+
[learn-nix]:https://fzakaria.com/2024/07/05/learn-nix-the-fun-way.html
18+

functions/what_is_my_ip/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/what_is_my_ip/flake.nix

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
description = "what is my ip";
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+
default = pkgs.writeShellScriptBin "entrypoint" ''
23+
${pkgs.curl}/bin/curl -s http://httpbin.org/get | \
24+
${pkgs.jq}/bin/jq --raw-output .origin
25+
'';
26+
};
27+
});
28+
}

functions/what_is_my_ip/runbook.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Background
2+
3+
The `write_file` function has wo parameters.
4+
5+
* `path`: is a relative path from some project_root
6+
* `content`: is the content that should be written into the file
7+
8+
## Usage
9+
10+
This function should be given a rw bind mount for the root of a project.
11+
12+
```sh
13+
docker run --rm --entrypoint /app/result/bin/entrypoint vonwig/what-is-my-ip:latest
14+
```
15+
16+
## Build
17+
18+
```sh
19+
docker build -t vonwig/what-is-my-ip:latest .
20+
```
21+
22+
```sh
23+
# docker:command=build
24+
25+
docker buildx build \
26+
--builder hydrobuild \
27+
--platform linux/amd64,linux/arm64 \
28+
--tag vonwig/what-is-my-ip:latest \
29+
--file Dockerfile \
30+
--push .
31+
docker pull vonwig/what-is-my-ip:latest
32+
```

0 commit comments

Comments
 (0)