Skip to content
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

Provide devcontainer #517

Merged
merged 37 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5ea047d
Enable dockerfile plugin in dprint
kachick Jun 24, 2023
358d774
WIP
kachick Jun 24, 2023
24e80a9
Devcontainer
kachick Jun 24, 2023
86f1550
Super hard!
kachick Jun 24, 2023
6a802bc
Merge branch 'main' into docker-or-devcontainer
kachick Jun 26, 2023
c4eff5d
Drop dprint-plugin-dockerfile to avoid the incorrect alerts
kachick Jun 26, 2023
4998b57
Integrate hadolint
kachick Jun 26, 2023
5f4f770
Looks like hadolint action does not consider dotfiles in file glob
kachick Jun 26, 2023
26c2a00
Remove testing code because of checked the hadolint behabvior in CI
kachick Jun 26, 2023
9b15d53
Finish basic of devcontainer...!
kachick Jun 26, 2023
1e21a0e
Add devcontainer CI
kachick Jun 26, 2023
d1571ac
Enable scheduled trigger for container CI
kachick Jun 26, 2023
4d01d2b
Try the PATH setting
kachick Jun 26, 2023
99474ae
I dont know why bin/sh used only in CI...
kachick Jun 26, 2023
cabc9aa
Remove outdaetd code
kachick Jun 26, 2023
1042544
Give it a try!
kachick Jun 26, 2023
ed5a3ae
Add debug log
kachick Jun 26, 2023
206e8ad
Looks like a same issue of https://github.com/devcontainers/ci/issues…
kachick Jun 26, 2023
af54fc3
We drop the mkdir phase again
kachick Jun 26, 2023
9ae97c7
Merge branch 'main' into docker-or-devcontainer
kachick Jun 26, 2023
481643a
Prefer 22.04
kachick Jun 26, 2023
826963d
Give it a try! (I guess, this will fail)
kachick Jun 27, 2023
051525f
I am tired
kachick Jun 27, 2023
471efad
Add more debugg logging
kachick Jun 27, 2023
feca185
Revert 826963d2819a89de49cfadcdfc10e73565e3ac9e
kachick Jun 27, 2023
8743170
I am a new in debugger;
kachick Jun 27, 2023
4a497cf
Persially revert 051525ff7317a500dac4424a67e06f7402910923
kachick Jun 27, 2023
23b7f15
Update UID/GID with hostmachine before installing NIX
kachick Jun 27, 2023
2a63dd7
Fix missing commit
kachick Jun 27, 2023
48e41e8
Restrict with whole timeout-minutes
kachick Jun 27, 2023
08743fc
Is this the correct shell specifier?
kachick Jun 27, 2023
5b4025f
This action can not simulate actuall devcontainer use :<
kachick Jun 27, 2023
3c14af5
`git checkout main dprint.json`
kachick Jun 27, 2023
31c3820
Fix a typo
kachick Jun 27, 2023
dc9977c
Merge branch 'main' into docker-or-devcontainer
kachick Jun 27, 2023
f8c454a
Fix merge miss
kachick Jun 27, 2023
34cec69
Update docs
kachick Jun 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Do not use `FROM nixos/nix`. It cannot use `groupadd` even not the NixOS
# See https://stackoverflow.com/questions/75653182/why-do-some-official-nix-docker-containers-not-have-the-nixos-rebuild-command
FROM mcr.microsoft.com/devcontainers/base:ubuntu-22.04

# Above microsoft image contains to create vscode user

# Use same UID/GID to avoid mount and nix path permission issues
ARG container_user_uid
ARG container_user_gid
RUN usermod -u $container_user_uid vscode && groupmod -g $container_user_gid vscode

# Switching the user should be after the changed IDs
USER vscode

# This just uses for following build steps.
# vscode will set the WORKDIR into `/workspaces/wait-other-job`
WORKDIR /home/vscode/sandbox

# The install scripts will be syntax error if it will be eval with dash
SHELL ["/bin/bash", "-c"]
Comment on lines +19 to +20
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NixOS/nix#4896 Looks same issue


# daemon mode required systemd, docker disables it
# As I understand the definition https://hub.docker.com/r/nixos/nix/dockerfile at Nix 2.17, it looks like a single user installation(no-daemon)
# See https://github.com/NixOS/nix/blob/fd4f03b8fdcb0f33552730c786139019e29f5dbe/scripts/install-nix-from-closure.sh#L129-L139
RUN bash <(curl -L https://nixos.org/nix/install) --no-daemon --yes
Comment on lines +22 to +25
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


ENV PATH /home/vscode/.nix-profile/bin:$PATH

# Basically do NOT merge files in COPY to prefer inline cache benefit

# Make sure the building phase done. To cache. `nix-build --no-out-link` does similar, but it skips bash.
# NOTE: This does not mean the entry point is in nix-shell.
COPY ["../default.nix", "./"]

# Do no include like `npm install` here. Cuurently I don't cache it in container
RUN nix-channel --update && nix-shell --run 'echo "Keep this line to build nix environment in this phase"'

COPY ["./.devcontainer/setup_direnv.bash", "./"]
RUN ./setup_direnv.bash

# Clean up to avoid confusion
# hadolint ignore=DL3059
RUN rm ./default.nix ./setup_direnv.bash

# `ENTRYPOINT` and `CMD` will be ignored in .devcontainer. Use postCreateCommand instead
# To simulate in docker, following CMD requires to specify PWD with repository files. Check `makers docker-shell`
ENTRYPOINT ["./.devcontainer/bootstrap.bash"]
CMD ["zsh"]
11 changes: 11 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## How to update path for each extentions of devcontainer.json?

Like this.

```console
nix-instantiate --eval -E '(import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/4ef0dd85324fca49bf06fd9f2d52711503b1128c.tar.gz") {}).pkgs.dprint.outPath' | tr -d '"'
```

You should update them after updating fetchTarball path in default.nix

I want to realize better integration, but I don't know it.
4 changes: 4 additions & 0 deletions .devcontainer/bootstrap.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

whoami && id && cat /etc/passwd && ls -alh /nix && ls -alh . && \
nix-shell --run 'direnv allow && makers setup'
62 changes: 62 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
"container_user_uid": "${localEnv:container_user_uid:1000}",
"container_user_gid": "${localEnv:container_user_gid:1000}"
}
},
"remoteUser": "vscode",
"containerUser": "vscode",
"updateRemoteUserUID": true,
"containerEnv": {
"HOME": "/home/vscode"
},
"userEnvProbe": "loginShell",
"postCreateCommand": "./.devcontainer/bootstrap.bash",
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
},
"terminal.integrated.defaultProfile.linux": "zsh",
"editor.defaultFormatter": "dprint.dprint",
"editor.formatOnSave": true,
"hadolint.hadolintPath": "/nix/store/3lrlw90virfykpbxv4n1mbap0g8593r5-hadolint-2.12.0/bin/hadolint",
"dprint.path": "/nix/store/6rzbms3q4p7yfp56c9vcr9ip83r1rl8m-dprint-0.36.1/bin/dprint",
"[nix]": {
"editor.defaultFormatter": "jnoortheen.nix-ide"
},
"json.schemas": [
{
"fileMatch": [
"dprint.json"
],
"url": "https://docs.renovatebot.com/renovate-schema.json"
}
],
"cSpell.words": [
"deno",
"dprint",
"OIDC",
"rmrf"
]
},
"extensions": [
"github.vscode-github-actions",
"dbaeumer.vscode-eslint",
"redhat.vscode-yaml",
"editorconfig.editorconfig",
"dprint.dprint",
"jnoortheen.nix-ide",
"tamasfe.even-better-toml",
"exiasr.hadolint",
"ms-azuretools.vscode-docker"
]
}
}
}
3 changes: 3 additions & 0 deletions .devcontainer/docker_build.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker build . --tag wait-other-jobs:latest --build-arg container_user_uid="$(stat -c '%u' .)" --build-arg container_user_gid="$(stat -c '%g' .)" --file .devcontainer/Dockerfile
3 changes: 3 additions & 0 deletions .devcontainer/docker_shell.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker run --interactive --tty --rm --volume "$(pwd):/workspaces/wait-other-job" --workdir "/workspaces/wait-other-job" wait-other-jobs:latest
4 changes: 4 additions & 0 deletions .devcontainer/setup_direnv.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

echo 'eval "$(nix-shell --run '"'"'direnv hook bash'"'"')"' >> ~/.bashrc
echo 'eval "$(nix-shell --run '"'"'direnv hook zsh'"'"')"' >> ~/.zshrc
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use_nix
44 changes: 44 additions & 0 deletions .github/workflows/ci-devcontainer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: 'CI - Devcontainer'
on:
push:
branches: [main]
paths:
- '.devcontainer/**'
- '.github/workflows/ci-devcontainer.yml'
- '*.nix'
- 'Makefile.toml'
- '**vscode**'
pull_request:
paths:
- '.devcontainer/**'
- '.github/workflows/ci-devcontainer.yml'
- '*.nix'
- 'Makefile.toml'
- '**vscode**'
schedule:
# About JST 8:00
- cron: '0 17 * * *'
workflow_dispatch:

jobs:
build:
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Log host users and file owners
run: |
whoami
id
ls -alh
- name: Set UID and GID as ENV from repository files
run: |
uid="$(stat -c '%u' .)"
gid="$(stat -c '%g' .)"
echo "container_user_uid=${uid}" | tee -a "$GITHUB_ENV"
echo "container_user_gid=${gid}" | tee -a "$GITHUB_ENV"
- name: Build and run dev container task
uses: devcontainers/[email protected]
with:
# Can't we call these comands on the nix-shell even enabled direnv...?
runCmd: nix-shell --run 'makers help && makers versions && makers check'
24 changes: 24 additions & 0 deletions .github/workflows/hadolint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: hadolint
on:
push:
branches: [main]
paths:
- '**Dockerfile'
- '**.dockerfile'
- '.github/workflows/hadolint.yml'
pull_request:
paths:
- '**Dockerfile'
- '**.dockerfile'
- '.github/workflows/hadolint.yml'

jobs:
lint:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: hadolint/[email protected]
with:
recursive: 'true'
dockerfile: .devcontainer/Dockerfile
5 changes: 4 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"recommendations": [
"github.vscode-github-actions",
"dbaeumer.vscode-eslint",
"redhat.vscode-yaml",
"editorconfig.editorconfig",
"dprint.dprint",
"jnoortheen.nix-ide",
"tamasfe.even-better-toml"
"tamasfe.even-better-toml",
"exiasr.hadolint",
"ms-azuretools.vscode-docker"
]
}
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"editor.defaultFormatter": "dprint.dprint",
"editor.formatOnSave": true,
"[nix]": {
Expand Down
28 changes: 26 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,41 @@

## Setup

### Nix

1. Install [Nix](https://nixos.org/) package manager
2. Run `nix-shell`
3. You can use development tasks

```console
> nix-shell
(prepared bash)
```

### Devcontainer/Docker

You can use Devcontainer or the Docker to skip installing Nix on your local machine.

[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/kachick/vwait-other-jobs) 🚶

Simulate devcontainer from docker as following.

```console
> ./.devcontainer/docker_build.bash
> ./.devcontainer/docker_shell.bash
vscode ➜ /workspaces/wait-other-job (main) $
vscode ➜ /workspaces/wait-other-job (main) $ makers check
...Done in...
```

### Tasks

```console
> makers help
Tools
----------
> Tools

---

check - ...
help - ...

Expand Down
16 changes: 16 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies = [
]
script = [
"actionlint",
"hadolint .devcontainer/Dockerfile",
"npm run lint:check",
]

Expand Down Expand Up @@ -77,6 +78,20 @@ category = "Tools"
description = 'Parallel runner for all tests and linters'
run_task = { name = ["typecheck", "test-ts", "test-js", "lint-check"], parallel = true }

[tasks.docker-build]
category = "Tools"
description = 'Build docker image for devcontainer'
script = [
"./.devcontainer/docker_build.bash",
]

[tasks.docker-shell]
category = "Tools"
description = 'Login to built container'
script = [
"./.devcontainer/docker_shell.bash",
]

[tasks.help]
category = "Tools"
description = 'Might help you - (This one)'
Expand All @@ -94,6 +109,7 @@ script = [
# Returnes NON 0, why...? :<
# "nixpkgs-fmt --version",
"actionlint --version",
"hadolint --version",
]

[tasks.check_no_git_diff]
Expand Down
2 changes: 2 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

pkgs.mkShell {
buildInputs = [
pkgs.direnv
pkgs.nodejs-16_x
pkgs.dprint
pkgs.cargo-make
pkgs.nil
pkgs.nixpkgs-fmt
pkgs.actionlint
pkgs.hadolint
];
}