Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ Keyword: macOS, Xcode, Homebrew, Emacs, Zsh
./scripts/devcontainer-check.sh
#+end_src

** GitHub Codespaces
- This repo includes a devcontainer definition at =.devcontainer/devcontainer.json=.
- On first launch, Codespaces runs =./scripts/devcontainer-check.sh= which:
- runs =nix flake check=
- applies the Home Manager configuration for =vscode@devcontainer=
- If you don't want your Codespace home directory dotfiles to be managed automatically, remove/disable =postCreateCommand= in =.devcontainer/devcontainer.json= and run the script manually when needed.

** GitHub Codespaces Dotfiles
- You can also use this repository as a Codespaces *Dotfiles* repository.
- When configured in GitHub settings, Codespaces clones this repo and runs =./install.sh=.
- On Linux (Codespaces), =install.sh= installs Nix (if needed) and applies the Home Manager configuration for the current user (e.g. =codespace@devcontainer=).
- Note: this repo uses a git submodule (=resources/zsh/.zsh/zaw=). =install.sh= initializes submodules automatically, but your environment must allow fetching it.

** CI
- GitHub Actions runs =nix flake check= on Ubuntu and macOS for every push and PR.

Expand Down
45 changes: 32 additions & 13 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@
hostConfig = import ./nix/hosts.nix;
username = hostConfig.username;
lib = nixpkgs.lib;
devcontainerHost = {
username = "vscode";
darwinHost = "devcontainer";
darwinSystem = hostConfig.darwinSystem;
wslHost = "devcontainer";
wslSystem = hostConfig.wslSystem;
};
devcontainerHosts = [
{
username = "vscode";
darwinHost = "devcontainer";
darwinSystem = hostConfig.darwinSystem;
wslHost = "devcontainer";
wslSystem = hostConfig.wslSystem;
}
# GitHub Codespaces often uses the "codespace" user (Dotfiles feature runs as the current user).
{
username = "codespace";
darwinHost = "devcontainer";
darwinSystem = hostConfig.darwinSystem;
wslHost = "devcontainer";
wslSystem = hostConfig.wslSystem;
}
];

mkPkgs = system:
import nixpkgs {
Expand Down Expand Up @@ -66,10 +76,17 @@
darwinConfigurations.${hostConfig.darwinHost} =
mkDarwin hostConfig.darwinSystem;

homeConfigurations."${username}@${hostConfig.wslHost}" =
mkHome hostConfig.wslSystem;
homeConfigurations."${devcontainerHost.username}@${devcontainerHost.wslHost}" =
mkHomeWith { system = devcontainerHost.wslSystem; hostCfg = devcontainerHost; };
homeConfigurations =
{
"${username}@${hostConfig.wslHost}" = mkHome hostConfig.wslSystem;
}
// lib.listToAttrs (map
(dc:
{
name = "${dc.username}@${dc.wslHost}";
value = mkHomeWith { system = dc.wslSystem; hostCfg = dc; };
})
devcontainerHosts);

checks = lib.genAttrs
(lib.unique [ hostConfig.wslSystem hostConfig.darwinSystem ])
Expand All @@ -78,8 +95,10 @@
then { darwin-config = (mkDarwin system).system; }
else {
home-wsl = (mkHome system).activationPackage;
home-devcontainer =
(mkHomeWith { system = system; hostCfg = devcontainerHost; }).activationPackage;
home-devcontainer-vscode =
(mkHomeWith { system = system; hostCfg = builtins.elemAt devcontainerHosts 0; }).activationPackage;
home-devcontainer-codespace =
(mkHomeWith { system = system; hostCfg = builtins.elemAt devcontainerHosts 1; }).activationPackage;
});
};
}
101 changes: 83 additions & 18 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,94 @@
#!/bin/bash
#!/usr/bin/env bash
set -euo pipefail

echo "Checking dotnet";
if !(type dotnet >/dev/null 2>&1); then
if [ ! -f /usr/local/share/dotnet/dotnet ]; then
echo "dotnet doesn't exsist."
./scripts/install-dotnet.sh
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT_DIR"

is_linux() { [[ "${OSTYPE:-}" == linux* ]]; }
is_darwin() { [[ "${OSTYPE:-}" == darwin* ]]; }

echo "Initializing submodules (if any)..."
if command -v git >/dev/null 2>&1 && [ -f "$ROOT_DIR/.gitmodules" ]; then
git submodule update --init --recursive
fi

if is_linux; then
echo "Linux detected: applying Nix/Home Manager config (Codespaces-friendly)."

if ! command -v nix >/dev/null 2>&1; then
echo "Nix not found; installing Nix..."
# Prefer Determinate Nix installer (works well in containers).
if command -v curl >/dev/null 2>&1; then
curl -fsSL https://install.determinate.systems/nix | sh -s -- install --no-confirm || true
fi

# Load Nix profile if installed.
if [ -e /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]; then
# shellcheck disable=SC1091
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
elif [ -e "${HOME}/.nix-profile/etc/profile.d/nix.sh" ]; then
# shellcheck disable=SC1091
. "${HOME}/.nix-profile/etc/profile.d/nix.sh"
fi

eval `/usr/libexec/path_helper -s`
fi

# Ensure flakes are enabled even on older Nix installs.
mkdir -p "${HOME}/.config/nix"
if [ ! -f "${HOME}/.config/nix/nix.conf" ] || ! grep -q "experimental-features" "${HOME}/.config/nix/nix.conf"; then
printf "\nexperimental-features = nix-command flakes\n" >> "${HOME}/.config/nix/nix.conf"
fi

if ! command -v nix >/dev/null 2>&1; then
echo "ERROR: nix command not available after install attempt." >&2
exit 1
fi

echo "Applying Home Manager configuration..."
current_user="$(id -un)"
target="${current_user}@devcontainer"
if nix --accept-flake-config run nixpkgs#home-manager -- switch --flake "$ROOT_DIR#${target}"; then
echo "Applied Home Manager for '$target'."
else
echo "WARNING: failed to apply '$target'; falling back to 'vscode@devcontainer'." >&2
nix --accept-flake-config run nixpkgs#home-manager -- switch --flake "$ROOT_DIR#vscode@devcontainer"
fi

echo "Done."
exit 0
fi

echo "Checking dotnet script";
if !(dotnet tool list -g | grep dotnet-script > /dev/null 2>&1); then
echo "Install dotnet-script";
if is_darwin; then
echo "macOS detected: running legacy (dotnet-script + Homebrew) installer."

echo "Checking dotnet"
if ! type dotnet >/dev/null 2>&1; then
if [ ! -f /usr/local/share/dotnet/dotnet ]; then
echo "dotnet doesn't exist."
./scripts/install-dotnet.sh
fi

eval "$(/usr/libexec/path_helper -s)"
fi

echo "Checking dotnet script"
if ! (dotnet tool list -g | grep dotnet-script > /dev/null 2>&1); then
echo "Install dotnet-script"
dotnet tool install -g dotnet-script
fi
fi

echo "Checking Homebrew"
if !(type brew > /dev/null); then
echo "Checking Homebrew"
if ! type brew >/dev/null 2>&1; then
if [ ! -d /opt/homebrew/bin ]; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

eval "$(/opt/homebrew/bin/brew shellenv)"
fi

echo "Run installation scripts"
dotnet script scripts/Main.csx
exit 0
fi

echo "Run installation scripts"
dotnet script scripts/Main.csx
echo "Unsupported OS: OSTYPE='${OSTYPE:-unknown}'" >&2
exit 1
4 changes: 2 additions & 2 deletions scripts/devcontainer-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -euo pipefail

echo "Running nix flake check..."
nix flake check
nix --accept-flake-config flake check

echo "Applying home-manager config for devcontainer..."
nix run nixpkgs#home-manager -- switch --flake .#devcontainer@devcontainer
nix --accept-flake-config run nixpkgs#home-manager -- switch --flake .#vscode@devcontainer