From 4f53bce3d30f1e8d4d1fd69879b1f7dd8e6b9bcc Mon Sep 17 00:00:00 2001 From: James Hodgkinson Date: Mon, 2 Dec 2024 21:02:56 +1000 Subject: [PATCH] Devcontainertainertainer (#3251) --- .devcontainer/devcontainer.json | 20 +++++++++++-- scripts/devcontainer_postcreate.sh | 46 ++++++++++++++++++++++++++++++ scripts/devcontainer_poststart.sh | 21 ++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100755 scripts/devcontainer_postcreate.sh create mode 100755 scripts/devcontainer_poststart.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 134ee4f6d..a106ad0a6 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,7 +2,8 @@ // README at: https://github.com/devcontainers/templates/tree/main/src/rust { "name": "Rust", - // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + + // https://github.com/devcontainers/images/tree/main/src/rust "image": "mcr.microsoft.com/devcontainers/rust:1-1-bookworm", "features": { }, @@ -13,6 +14,11 @@ "source": "devcontainer-cargo-cache-${devcontainerId}", "target": "/usr/local/cargo", "type": "volume" + }, + { + "source": "devcontainer-sccache-cache-${devcontainerId}", + "target": "/home/vscode/.cache/sccache", + "type": "volume" } ], // Features to add to the dev container. More info: https://containers.dev/features. @@ -21,7 +27,17 @@ // Use 'forwardPorts' to make a list of ports inside the container available locally. "forwardPorts": [8443], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "rustup update && rustup default stable && rustup component add rustfmt clippy && sudo apt-get update && sudo apt-get install -y sccache ripgrep libssl-dev pkg-config jq libpam0g-dev libudev-dev cmake build-essential && cargo install cargo-audit mdbook-mermaid mdbook && cargo install mdbook-alerts --version 0.6.4 && cargo install deno --locked" + "postCreateCommand": "./scripts/devcontainer_postcreate.sh", + + // postStartCommand runs things once the container's up, in this case, we're starting sccache + "postStartCommand": "./scripts/devcontainer_poststart.sh", + + // environment variables + "containerEnv": { + // "RUSTC_WRAPPER": "sccache", + // "CC":"/usr/bin/sccache /usr/bin/clang", + "SCCACHE_SERVER_UDS" : "/tmp/sccache.sock" + } // Configure tool-specific properties. // "customizations": {}, diff --git a/scripts/devcontainer_postcreate.sh b/scripts/devcontainer_postcreate.sh new file mode 100755 index 000000000..93ac5d4ec --- /dev/null +++ b/scripts/devcontainer_postcreate.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +set -e + +echo "Installing rust stable toolchain" +rustup update +rustup default stable +rustup component add rustfmt clippy + +echo "Installing packages" +sudo apt-get update +sudo apt-get install -y \ + build-essential \ + clang \ + cmake \ + libtss2-dev \ + tpm-udev \ + jq \ + libpam0g-dev \ + libssl-dev \ + libudev-dev \ + pkg-config \ + ripgrep + +export PATH="$HOME/.cargo/bin:$PATH" + +cargo install \ + sccache + + +# stupid permissions issues +sudo chown vscode ~/ -R +sudo chgrp vscode ~/ -R + +# shellcheck disable=SC1091 +source scripts/devcontainer_poststart.sh + +cargo install + cargo-audit \ + mdbook-mermaid \ + mdbook +cargo install mdbook-alerts --version 0.6.4 +cargo install deno --locked + + +echo "Done!" \ No newline at end of file diff --git a/scripts/devcontainer_poststart.sh b/scripts/devcontainer_poststart.sh new file mode 100755 index 000000000..f993b51c8 --- /dev/null +++ b/scripts/devcontainer_poststart.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# stupid permissions issues +sudo chown vscode ~/ -R +sudo chgrp vscode ~/ -R + +export PATH="$HOME/.cargo/bin:$PATH" +SCCACHE_SERVER_UDS="/tmp/sccache.sock" sccache --start-server + +# to set up sccache etc +if [ "$(grep -c "devcontainer_poststart" ~/.bashrc)" -eq 0 ]; then + echo "adding devcontainer_poststart to bashrc" + echo "source /workspaces/kanidm/scripts/devcontainer_poststart.sh" >> ~/.bashrc +fi +export RUSTC_WRAPPER="sccache" +export CC="sccache /usr/bin/clang" + +# disable incremental builds +# cargo docs: https://doc.rust-lang.org/cargo/reference/profiles.html#incremental +# sccache docs on why to disable incremental builds: https://github.com/mozilla/sccache#known-caveats +export CARGO_INCREMENTAL=false