Skip to content

Instantly share code, notes, and snippets.

@heywoodlh
Last active November 7, 2024 23:00
Show Gist options
  • Save heywoodlh/b7bd4b4d067dd93402b04015a0ee3531 to your computer and use it in GitHub Desktop.
Save heywoodlh/b7bd4b4d067dd93402b04015a0ee3531 to your computer and use it in GitHub Desktop.
Script for installing Linux environments
use flake
lefthook install

Dependencies

Debian:

apt update && apt install -y curl sudo git

Alpine:

apk add --no-cache curl bash shadow sudo git

Arch:

pacman -Syu --noconfirm curl bash sudo git

Usage:

Ensure the following commands are run as a sudo user.

Workstation:

curl -L https://files.heywoodlh.io/scripts/linux.sh | bash -s -- workstation --ansible --home-manager

Server:

curl -L https://files.heywoodlh.io/scripts/linux.sh | bash -s -- server --ansible --home-manager

Unprivileged, FUSE-enabled system (installs AppImages):

curl -L https://files.heywoodlh.io/scripts/linux.sh | bash -s -- files-only

Working with the Gist:

Source: https://gist.github.com/heywoodlh/b7bd4b4d067dd93402b04015a0ee3531

git clone [email protected]:b7bd4b4d067dd93402b04015a0ee3531.git linux-scripts
#!/usr/bin/env bash
set -ex
# Ansible and Home Manager
sudo -u heywoodlh /linux.sh workstation --ansible --home-manager || exit 1
#!/usr/bin/env bash
set -ex
# Ansible and Home Manager
sudo -u heywoodlh /linux.sh server --ansible --home-manager || exit 1
# Ubuntu target
FROM docker.io/heywoodlh/systemd:ubuntu AS ubuntu-base
COPY ./linux.sh /linux.sh
COPY docker-server.sh /server.sh
COPY docker-desktop.sh /desktop.sh
RUN apt update && apt install -y sudo git \
&& useradd -ms /bin/bash heywoodlh
COPY sudo-heywoodlh /etc/sudoers.d/heywoodlh
#ENV ANSIBLE_REMOTE_TMP="/tmp/ansible"
USER heywoodlh
ENTRYPOINT [ "sudo", "/docker-entrypoint.sh" ]
# Ubuntu server
FROM ubuntu-base AS ubuntu-server
CMD [ "/server.sh"]
# Ubuntu desktop
FROM ubuntu-base AS ubuntu-desktop
CMD [ "/desktop.sh"]
# Debian target
FROM docker.io/heywoodlh/systemd:debian AS debian-base
COPY ./linux.sh /linux.sh
COPY docker-server.sh /server.sh
COPY docker-desktop.sh /desktop.sh
RUN apt update && apt install -y sudo git \
&& useradd -ms /bin/bash heywoodlh
COPY sudo-heywoodlh /etc/sudoers.d/heywoodlh
#ENV ANSIBLE_REMOTE_TMP="/tmp/ansible"
USER heywoodlh
ENTRYPOINT [ "sudo", "/docker-entrypoint.sh" ]
# Debian server
FROM debian-base AS debian-server
CMD [ "/server.sh"]
# Debian desktop
FROM debian-base AS debian-desktop
CMD [ "/desktop.sh"]
# Alpine target
FROM docker.io/heywoodlh/openrc AS alpine-base
COPY ./linux.sh /linux.sh
COPY docker-server.sh /server.sh
COPY docker-desktop.sh /desktop.sh
RUN apk add bash sudo git \
&& addgroup -S heywoodlh && adduser -S heywoodlh -G heywoodlh
COPY sudo-heywoodlh /etc/sudoers.d/heywoodlh
#ENV ANSIBLE_REMOTE_TMP="/tmp/ansible"
USER heywoodlh
ENTRYPOINT [ "sudo", "/openrc.sh" ]
# Alpine server
FROM alpine-base AS alpine-server
CMD [ "/server.sh"]
# Alpine desktop
FROM alpine-base AS alpine-desktop
CMD [ "/desktop.sh"]
# Arch Linux target
FROM docker.io/heywoodlh/systemd:archlinux AS archlinux-base
COPY ./linux.sh /linux.sh
COPY docker-server.sh /server.sh
COPY docker-desktop.sh /desktop.sh
RUN pacman -Sy --noconfirm sudo git
COPY sudo-heywoodlh /etc/sudoers.d/heywoodlh
#ENV ANSIBLE_REMOTE_TMP="/tmp/ansible"
USER heywoodlh
ENTRYPOINT [ "sudo", "/docker-entrypoint.sh" ]
# Arch Linux server
FROM archlinux-base AS archlinux-server
CMD [ "/server.sh"]
# Arch Linux desktop
FROM archlinux-base AS archlinux-desktop
CMD [ "/desktop.sh"]
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1717464529,
"narHash": "sha256-cvNZu6TQ4nCJEVA6rRE35xMyU/m04HDJWfixPC0I9AM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "5a7646f59010bf91d68cf323bf3ae8ca5d31d3ef",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
{
description = "linux-scripts dev shell";
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = inputs @ {
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
devShell = pkgs.mkShell {
name = "linux-scripts-dev";
buildInputs = with pkgs; [
lefthook
];
};
formatter = pkgs.alejandra;
});
}
pre-commit:
commands:
upload-fastmail:
run: |
./upload.sh
#!/usr/bin/env bash
# Script to setup bare-minimum Linux env
if ! uname | grep -iq 'Linux'
then
echo "This script must be run on a Linux system"
exit 1
fi
print-usage () {
printf "\nUsage: %s [workstation|server|files-only] [--ansible --home-manager]\n" "$0"
exit 0
}
[[ $# -eq 0 ]] && print-usage
# Flakes variable for all things to reference
flakes="vim zellij git helix"
# Exit if $1 doesn't exist or is not workstation/server/files-only
system="$1"
[[ "${system}" != "workstation" && "${system}" != "server" && "${system}" != "files-only" ]] && print-usage
if [[ "${system}" == "files-only" ]]
then
# Install appimages only and place them in $HOME/bin
mkdir -p $HOME/bin
if command -v curl &> /dev/null
then
download_file () {
url="$1"
dest="$2"
[[ ! -e "${dest}" ]] && echo "Downloading: $url => $dest" && curl --silent -L "${url}" -o "${dest}"
}
else
echo "Please install curl and re-run this script"
exit 1
fi
# Download flakes
for flake in $flakes
do
download_file "https://github.com/heywoodlh/flakes/releases/download/appimages/${flake}-$(arch).appimage" "$HOME/bin/${flake}" \
&& chmod +x "${HOME}/bin/${flake}" \
&& echo "${HOME}/bin/${flake}" installed
done
# Helix symlink
[[ -e "${HOME}/bin/helix" ]] && [[ ! -e "${HOME}/bin/hx" ]] && ln -sv "${HOME}/bin/helix" "${HOME}/bin/hx"
if ! grep -q 'heywoodlh-PATH' "${HOME}/.profile" &>/dev/null
then
echo "Setting up PATH in ${HOME}/.profile"
printf "#heywoodlh-PATH\nexport PATH=\"$HOME/bin:$PATH\"" >> ${HOME}/.profile
fi
else
# If --ansible provided, set ansible=true
echo "$@" | grep -q '\-\-ansible' && ansible=true
# If --home-manager provided, set home-manager=true
echo "$@" | grep -q '\-\-home-manager' && home_manager=true
# Install Nix
if grep -q 'Alpine Linux' /etc/os-release
then
# Enable the community repository
sudo sed -i 's/#\(.*\/community\)/\1/' /etc/apk/repositories
# Install Nix package
sudo apk add --no-cache nix shadow
# Add user to nix group
sudo usermod -aG nix $USER
# Configure nix
grep flakes /etc/nix/nix.conf || printf "extra-experimental-features = nix-command flakes" | sudo tee -a /etc/nix/nix.conf
# Enable nix-daemon
sudo rc-update add nix-daemon
sudo rc-service nix-daemon restart
elif grep -q 'Arch Linux' /etc/os-release
then
sudo pacman -Sy --noconfirm nix
sudo systemctl enable --now nix-daemon.service
sudo usermod -aG nix-users $USER
else
if [ ! -d /nix/var/nix ]
then
echo "Installing Nix"
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm
sudo chown -R "$EUID" /nix
fi
fi
# Source nix-daemon.sh
[ -e /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ] && . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
install-nix-package () {
package_bin="$1"
package_source="$2"
unfree="$3"
exists=false
ls -l "$HOME/.nix-profile/bin/${package_bin}" &>/dev/null && exists=true
if [[ "${exists}" == "false" ]]
then
if [[ -n "${unfree}" ]]
then
echo "Installing unfree package ${package_source}"
NIXPKGS_ALLOW_UNFREE=1 nix profile install "${package_source}" --impure
else
echo "Installing package ${package_source}"
nix profile install "${package_source}" --no-write-lock-file
fi
else
echo "Package ${package_source} already exists"
fi
}
if [[ "${home_manager}" != "true" ]]
then
echo "Installing standalone packages"
# Install flakes
for flake in $flakes
do
install-nix-package "${flake}" "github:heywoodlh/flakes#${flake}"
done
# Configure Desktop
if [[ "${system}" == "workstation" ]]
then
echo "Configuring GNOME"
nix run "github:heywoodlh/flakes?dir=gnome"
echo "Configuring Firefox"
nix run "github:heywoodlh/flakes?dir=firefox#firefox-setup"
fi
# Install 1password
install-nix-package "op" "github:heywoodlh/flakes?dir=1password" "unfree"
[[ "${system}" == "workstation" ]] && install-nix-package "1password" "nixpkgs#_1password-gui" "unfree"
[[ "${system}" == "workstation" ]] && mkdir -p ~/.config/autostart && ln -s ~/.nix-profile/share/applications/1password.desktop ~/.config/autostart/1password.desktop &>/dev/null
[[ "${system}" == "workstation" ]] && nix run "github:heywoodlh/flakes?dir=1password#op-desktop-setup" && chmod u+w ~/.config/1Password/settings/settings.json
# Install Lima for Docker
install-nix-package "lima" "nixpkgs#lima"
install-nix-package "docker" "nixpkgs#docker-client"
else
if [[ $EUID -eq 0 ]]
then
echo "Script was invoked as root, skipping home-manager"
exit 1
else
# If WSL
if which wsl.exe &>/dev/null
then
echo "WSL detected, starting dconf manually"
export $(dbus-launch)
fi
if [[ "${system}" == "workstation" ]]
then
echo "Installing home-manager desktop configuration"
nix run "github:heywoodlh/nixos-configs/$(nix run nixpkgs#git -- ls-remote https://github.com/heywoodlh/nixos-configs | head -1 | awk '{print $1}')#homeConfigurations.heywoodlh.activationPackage" --impure --no-write-lock-file
fi
if [[ ${system} == "server" ]]
then
echo "Installing home-manager server configuration"
nix run "github:heywoodlh/nixos-configs/$(nix run nixpkgs#git -- ls-remote https://github.com/heywoodlh/nixos-configs | head -1 | awk '{print $1}')#homeConfigurations.heywoodlh-server.activationPackage" --impure --no-write-lock-file
fi
fi
fi
# Run ansible playbooks
if [[ "${ansible}" == "true" ]]
then
echo "Running ansible playbooks"
nix run "github:heywoodlh/flakes/$(git ls-remote https://github.com/heywoodlh/flakes | head -1 | awk '{print $1}')?dir=ansible#${system}"
fi
# If WSL
if which wsl.exe &>/dev/null
then
echo "WSL detected, adding WSL configuration"
mkdir -p $HOME/bin
cat > $HOME/bin/windows-firefox-setup << EOL
#!/usr/bin/env bash
drive="\$(cmd.exe /c "<nul set /p=%UserProfile%" 2>/dev/null | cut -d':' -f1 | tr [:upper:] [:lower:])"
firefox_profile="/mnt/\${drive}/\$(cmd.exe /c "<nul set /p=%UserProfile%" 2>/dev/null | sed 's/\\\/\//g' | cut -d':' -f2)/AppData/Roaming/Mozilla/Firefox/Profiles"
nix --extra-experimental-features "flakes nix-command" run "github:heywoodlh/flakes?dir=firefox#firefox-setup" -- "\${firefox_profile}"
EOL
chmod +x "$HOME/bin/windows-firefox-setup"
fi
# Symlink vim if it doesn't exist
[[ ! -e /usr/local/bin/vim ]] && [[ -e "${HOME}/.nix-profile/bin/vim" ]] && sudo ln -s "${HOME}/.nix-profile/bin/vim" /usr/local/bin/vim &>/dev/null || true
# Symlink Helix if it doesn't exist
[[ ! -e /usr/local/bin/hx ]] && [[ -e "${HOME}/.nix-profile/bin/hx" ]] && sudo ln -s "${HOME}/.nix-profile/bin/hx" /usr/local/bin/hx &>/dev/null || true
fi
heywoodlh ALL=(ALL) NOPASSWD: ALL
#!/usr/bin/env bash
export LC_ALL="C.UTF-8"
dir=$(dirname -- "$( readlink -f -- "$0"; )";)
operating_systems=("ubuntu" "debian" "alpine")
# If AMD64, also test Arch Linux
[[ $(arch) == "x86_64" ]] && operating_systems+=("archlinux")
# If argument provided, test single OS
if [[ -n "$1" ]]
then
printf '%s\0' "${operating_systems[@]}" | grep -q -F -x -z -- "$1" && operating_systems=("$1")
fi
echo "Operating systems that will be tested: ${operating_systems[@]}"
for os in "${operating_systems[@]}"
do
echo "Testing: ${os}"
set -ex
docker build -q -t ${os}-server -f ${dir}/Dockerfile --target ${os}-server ${dir} || printf "Error occurred on operating system: ${os}"
docker build -q -t ${os}-desktop -f ${dir}/Dockerfile --target ${os}-desktop ${dir} || printf "Error occurred on operating system: ${os}"
#mkdir -p /tmp/ansible
docker run -it --rm -v /tmp/.ansible:/root/.ansible --privileged ${os}-server
docker run -it --rm -v /tmp/.ansible:/root/.ansible --privileged ${os}-desktop
done
#!/usr/bin/env bash
# Get list of files, minus the script itself
script_dir="$(echo "$(dirname $0)")"
files="linux.sh README.md"
username="$(op-wrapper.sh item get "3qaxsqbv5dski4wqswxapc7qoi" --fields label=username)"
password="$(op-wrapper.sh item get "3qaxsqbv5dski4wqswxapc7qoi" --fields label=webdav --reveal)"
endpoint="https://myfiles.fastmail.com/files/scripts"
[[ -z "${username}" ]] && echo "Username not found" && exit 1
[[ -z "${password}" ]] && echo "Password not found" && exit 1
for file in ${files}
do
echo "Uploading ${file} to ${endpoint}/${file}"
curl --user "${username}:${password}" -T "${script_dir}/${file}" "${endpoint}/${file}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment