Skip to content

Commit

Permalink
Install nixos
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvapiav committed Mar 15, 2024
1 parent 553b785 commit 357d293
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/actions/build-n-upload-action/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ ENV TZ=Europe/Helsinki

RUN apt-get update && apt-get -y install curl xz-utils git

# Install nixos
COPY install_nix.sh /install_nix.sh
RUN /install_nix.sh

# Copies your code file from your action repository to the filesystem path `/` of the container
COPY entrypoint.sh /entrypoint.sh

Expand Down
101 changes: 101 additions & 0 deletions .github/actions/build-n-upload-action/install_nix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash

# Based on install-nix-action by Cachix.
# See:
# https://github.com/cachix/install-nix-action/blob/master/install-nix.sh

# shellcheck shell=bash

NIX_VERSION='2.18.1'

set -euo pipefail

# Check if Nix is already installed
if nix_path="$(type -p nix || command -v nix)"; then
echo "Aborting: Nix is already installed at ${nix_path}"
exit
fi

echo "Installing Nix version ${NIX_VERSION}"

# Create a temporary workdir
workdir=$(mktemp -d)
trap 'rm -rf "$workdir"' EXIT

# Helper function for writing Nix configuration
function add_config() {
[[ -n $1 ]] && echo "$1" >>"${workdir}/nix.conf"
}

# Add header comment
add_config "# Nix configuration reference:"
add_config "# https://nixos.org/manual/nix/stable/command-ref/conf-file.html"

# Basic config
add_config "experimental-features = nix-command flakes repl-flake"
add_config "system-features = nixos-test benchmark big-parallel kvm"
add_config "use-xdg-base-directories = true"
add_config "extra-nix-path = nixpkgs=flake:nixpkgs"

# Allow binary caches for user
add_config "trusted-users = root ${USER-}"

# Add default NixOS binary cache
add_config "substituters = https://cache.nixos.org/"
add_config "trusted-substituters = https://cache.nixos.org/"
add_config "trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
add_config "substitute = true"

# Optimize store disk usage
# add_config "auto-optimise-store = true"

# Set GC thresholds to instruct Nix to do GC if available disk-space
# in '/nix/store' drops below 'min-free' bytes during build.
# Nix keeps doing GC until there is no more garbage, or until 'max-free'
# bytes of disk space is available.
# add_config "min-free = $((25 * 1024 * 1024 * 1024))"
# add_config "max-free = $((50 * 1024 * 1024 * 1024))"

# Keep compilers, derivations and such when running GC.
# Also show stack trace on evaluation errors.
# add_config "keep-outputs = true"
# add_config "keep-derivations = true"
add_config "show-trace = true"

# Enable sandbox feature
add_config "sandbox = true"

# Maximum number of Nix jobs to build in parallel, 'auto' uses all available CPUs.
add_config "max-jobs = auto"

# This option controls the the parallelism of the builders, enabling
# parallel building at the discretion of the builders (by setting the
# NIX_BUILD_CORES env variable). For example for GNU Make this works
# just like passing the '-jN' flag to Make. Setting of '0' means
# use all available CPUs.
add_config "cores = 0"

unset -f add_config

# Nix installer flags
installer_options=(
--nix-extra-conf-file "${workdir}/nix.conf"
--daemon
--yes
)

echo "Nix installer options:"
echo "${installer_options[*]}"

# There is --retry-on-errors, but only newer curl versions support that
curl_retries=5
while ! curl -sS -o "${workdir}/install" -v --fail -L "https://releases.nixos.org/nix/nix-${NIX_VERSION}/install"; do
sleep 1
((curl_retries--))
if [[ ${curl_retries} -le 0 ]]; then
echo "curl retries failed" >&2
exit 1
fi
done

sh "${workdir}/install" "${installer_options[@]}"

0 comments on commit 357d293

Please sign in to comment.