Skip to content

Commit

Permalink
Add Dev Container Configuration Files
Browse files Browse the repository at this point in the history
  • Loading branch information
benz0li committed Sep 11, 2023
1 parent 1094cdc commit bef8f74
Show file tree
Hide file tree
Showing 15 changed files with 514 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .devcontainer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
*

!/assets/
!/assets/cradles/
!/assets/cradles/cabal/
!/assets/cradles/stack/
!/assets/screenshots/
!/bind-mounts/
!/conf/
!/conf/etc/
!/conf/etc/stack/
!/ghc-*/
!/scripts/
!/scripts/usr/
!/scripts/usr/local/
!/scripts/usr/local/bin/

!/assets/cradles/cabal/hie.yaml
!/assets/cradles/stack/hie.yaml
!/assets/screenshots/manageHLS.png
!/conf/etc/stack/config.yaml
!/ghc-*/devcontainer.json
!/scripts/usr/local/bin/*.sh

!/devcontainer.json
!/GHC.Dockerfile
!/LICENSE
!/README.md

!.gitignore
!.keep
103 changes: 103 additions & 0 deletions .devcontainer/GHC.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
ARG BUILD_ON_IMAGE=glcr.b-data.ch/ghc/ghc-musl
ARG GHC_VERSION=latest
ARG HLS_VERSION
ARG STACK_VERSION

ARG HLS_GHC_VERSION=${HLS_VERSION:+$GHC_VERSION}
ARG HLS_SFX=/${HLS_GHC_VERSION:-all}/hls:${HLS_VERSION:-none}

ARG STACK_VERSION_OVERRIDE=${STACK_VERSION:-none}

FROM ${BUILD_ON_IMAGE}:${GHC_VERSION} as files

RUN mkdir /files

COPY conf /files
COPY scripts /files

## Ensure file modes are correct
RUN find /files -type d -exec chmod 755 {} \; \
&& find /files -type f -exec chmod 644 {} \; \
&& find /files/usr/local/bin -type f -exec chmod 755 {} \;

FROM glcr.b-data.ch/commercialhaskell/ssi:${STACK_VERSION_OVERRIDE} as ssi

FROM ${BUILD_ON_IMAGE}${HLS_SFX} as hls

FROM docker.io/koalaman/shellcheck:stable as sci

FROM ${BUILD_ON_IMAGE}:${GHC_VERSION}

COPY --from=files /files /

RUN sysArch="$(uname -m)" \
## Ensure that common CA certificates
## and OpenSSL libraries are up to date
&& apk upgrade --no-cache \
ca-certificates \
openssl-dev \
## Install hadolint
&& case "$sysArch" in \
x86_64) tarArch="x86_64" ;; \
aarch64) tarArch="arm64" ;; \
*) echo "error: Architecture $sysArch unsupported"; exit 1 ;; \
esac \
&& apiResponse="$(curl -sSL \
https://api.github.com/repos/hadolint/hadolint/releases/latest)" \
&& downloadUrl="$(echo "$apiResponse" | grep -e \
"browser_download_url.*Linux-$tarArch\"" | cut -d : -f 2,3 | tr -d \")" \
&& echo "$downloadUrl" | xargs curl -sSLo /usr/local/bin/hadolint \
&& chmod 755 /usr/local/bin/hadolint \
## Create folders in root directory
&& mkdir -p /root/.local/bin \
## Create folders in skeleton directory
&& mkdir -p /etc/skel/.local/bin

## Update environment
ARG USE_ZSH_FOR_ROOT
ARG SET_LANG
ARG SET_TZ

ENV TZ=${SET_TZ:-$TZ} \
LANG=${SET_LANG:-$LANG}

## Change root's shell to ZSH
RUN if [ -n "$USE_ZSH_FOR_ROOT" ]; then \
apk add --no-cache zsh shadow; \
fix-chsh.sh; \
chsh -s /bin/zsh; \
fi \
## Update timezone if needed
&& if [ "$TZ" != "" ]; then \
apk add --no-cache tzdata; \
echo "Setting TZ to $TZ"; \
ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime \
&& echo "$TZ" > /etc/timezone; \
fi \
## Add/Update locale if needed
&& if [ "$LANG" != "C.UTF-8" ]; then \
if [ -n "$LANG" ]; then \
apk add --no-cache musl-locales musl-locales-lang; \
fi; \
sed -i "s/LANG=C.UTF-8/LANG=$LANG/" /etc/profile.d/*locale.sh; \
sed -i "s/LANG:-C.UTF-8/LANG:-$LANG/" /etc/profile.d/*locale.sh; \
sed -i "s/LC_COLLATE=C/LC_COLLATE=$LANG/" /etc/profile.d/*locale.sh; \
sed -i "s/LC_COLLATE:-C/LC_COLLATE:-$LANG/" /etc/profile.d/*locale.sh; \
fi

## Copy binaries as late as possible to avoid cache busting
COPY --from=ssi /usr/local /usr/local
COPY --from=hls /usr/local /usr/local
COPY --from=sci --chown=root:root /bin/shellcheck /usr/local/bin

ARG HLS_VERSION
ARG STACK_VERSION

ARG STACK_VERSION_OVERRIDE=${STACK_VERSION}

ENV HLS_VERSION=${HLS_VERSION} \
STACK_VERSION=${STACK_VERSION_OVERRIDE:-$STACK_VERSION}

RUN if [ "${GHC_VERSION%.*}" = "9.2" ]; then \
mv -f /usr/local/bin/stack /usr/bin; \
fi
35 changes: 35 additions & 0 deletions .devcontainer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Copyright (c) 2023 Olivier Benz

The code in this directory is not part of Stack (the software) and, with the
exceptions noted below, is distributed under the terms of the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

This directory also contains code with other copyrights. The affected files,
their copyrights and license statements are listed below.

--------------------------------------------------------------------------------
scripts/fix-chsh.sh
Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT License. See
https://github.com/devcontainers/features/blob/main/LICENSE for
license information.

--------------------------------------------------------------------------------
81 changes: 81 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Dev Containers

These Dev Containers are based on the same docker images that are used to build
the *statically linked* Linux amd64 and arm64 binary releases of stack.

Those multi-arch (`linux/amd64`, `linux/arm64/v8`) docker images themselves are
based on Alpine Linux and contain *unofficial* builds of GHC.

Only use the GHC available in the Dev Containers, because

1. the *official* GHC bindists for Alpine Linux (`x86_64`) are just too buggy.
2. there are currently (2023-09-05) no bindists for Alpine Linux (`AArch64`).

Therefore, flags `--system-ghc` and `--no-install-ghc` are set system-wide in
`/etc/stack/config.yaml`.

## Usage

For use with Github Codespaces, please follow the instruction at
[Creating a codespace for a repository](https://docs.github.com/en/codespaces/developing-in-codespaces/creating-a-codespace-for-a-repository#creating-a-codespace-for-a-repository).

For local/'remote host' usage with VS Code, please follow the instructions at
[Developing inside a Container](https://code.visualstudio.com/docs/devcontainers/containers).

## Build Stack

### Using cabal

:information_source: Default Dev Container only.

Command `cabal build` to build the `stack` executable.

Append `--flag=static` to build a *statically linked* `stack` executable that
can run on any Linux machine of the same architecture.

### Using Stack

Command `stack build` to build the `stack` executable.

Append `--flag=stack:static` to build a *statically linked* `stack` executable
that can run on any Linux machine of the same architecture.

Append `--stack-yaml stack-ghc-$GHC_VERSION.yaml` if you want to use an
experimental project-level configuration with the appropriate Dev Container.

## Haskell Language Server (HLS)

The
[Haskell Language Server](https://github.com/haskell/haskell-language-server)
is only available in the default Dev Container.

The VS Code
[Haskell](https://marketplace.visualstudio.com/items?itemName=haskell.haskell)
extension is not installed by default.

In order to use the Haskell extension, you must first configure the project for
the build tool of your choice:

### Stack

Place the cradle ([hie.yaml](assets/cradles/stack/hie.yaml)) for Stack in the
root of the workspace: `cp -f .devcontainer/assets/cradles/stack/hie.yaml .`

Command `stack build` once. `stack build` causes Cabal (the library) to create
the automatically generated module `Stack_build`.

### Cabal

Place the cradle ([hie.yaml](assets/cradles/cabal/hie.yaml)) for Cabal in the
root of the workspace: `cp -f .devcontainer/assets/cradles/cabal/hie.yaml .`

Command `cabal build` once. `cabal build` causes Cabal (the library) to create
the automatically generated module `Stack_build`.

### Haskell extension

Now, you can install and use the Haskell extension.

Choose `Manually via PATH` when asked the following question:

<img width="520" alt="manageHLS" src="assets/screenshots/manageHLS.png">
10 changes: 10 additions & 0 deletions .devcontainer/assets/cradles/cabal/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cradle:
cabal:
- path: "./src"
component: "lib:stack"
- path: "./app"
component: "stack:exe:stack"
- path: "./tests/integration"
component: "stack:exe:stack-integration-test"
- path: "./tests/unit"
component: "stack:test:stack-unit-test"
10 changes: 10 additions & 0 deletions .devcontainer/assets/cradles/stack/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cradle:
stack:
- path: "./src"
component: "stack:lib"
- path: "./app"
component: "stack:exe:stack"
- path: "./tests/integration"
component: "stack:exe:stack-integration-test"
- path: "./tests/unit"
component: "stack:test:stack-unit-test"
Binary file added .devcontainer/assets/screenshots/manageHLS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added .devcontainer/bind-mounts/.keep
Empty file.
4 changes: 4 additions & 0 deletions .devcontainer/conf/etc/stack/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Use only the GHC available on the PATH
system-ghc: true
# Do not automatically install GHC when necessary
install-ghc: false
60 changes: 60 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "Default",
"build": {
"dockerfile": "GHC.Dockerfile",
"args": {
"GHC_VERSION": "9.4.6",
"HLS_VERSION": "2.1.0.0",
"USE_ZSH_FOR_ROOT": "unset-to-use-ash",
"SET_LANG": "C.UTF-8",
"SET_TZ": ""
}
},

"onCreateCommand": "onCreateCommand.sh",
"postCreateCommand": "cabal update",

"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"configureZshAsDefaultShell": true,
"upgradePackages": false,
"username": "vscode",
"userUid": "automatic",
"userGid": "automatic"
}
},

"customizations": {
"vscode": {
"extensions": [
"[email protected]",
"exiasr.hadolint",
"GitHub.vscode-pull-request-github",
"mhutchie.git-graph",
"mutantdino.resourcemonitor",
"esbenp.prettier-vscode",
"timonwong.shellcheck"
],
"settings": {
"gitlens.showWelcomeOnInstall": false,
"gitlens.showWhatsNewAfterUpgrades": false,
"haskell.manageHLS": "PATH",
"resmon.show.battery": false,
"resmon.show.cpufreq": false
}
}
},

// Set 'remoteUser' to 'root' to connect as root instead.
"remoteUser": "vscode",
"mounts": [
"source=stack-default-home-vscode,target=/home/vscode,type=volume"
// "source=${localWorkspaceFolder}/.devcontainer/bind-mounts/stack-default-home-vscode,target=/home/vscode,type=bind"
]

// "remoteUser": "root",
// "mounts": [
// "source=stack-default-root,target=/root,type=volume"
// // "source=${localWorkspaceFolder}/.devcontainer/bind-mounts/stack-default-root,target=/root,type=bind"
// ]
}
Loading

0 comments on commit bef8f74

Please sign in to comment.