-
Notifications
You must be signed in to change notification settings - Fork 16
/
install.sh
executable file
·50 lines (41 loc) · 1.64 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
set -euo pipefail
# Install extra-container on a non-NixOS systemd-based system for use with sudo.
# Requires a multi-user nix installation, because extra-container runs nix-build as root.
# This script is idempotent.
[[ -e /run/booted-system/nixos-version ]] && isNixos=1 || isNixos=
[[ -e /run/systemd/system ]] && hasSystemd=1 || hasSystemd=
scriptDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
if [[ $EUID == 0 ]]; then
echo "This script should NOT be run as root."
exit 1
fi
if [[ $isNixos ]]; then
echo "This install script is not needed on NixOS. See the README for installation instructions."
exit 1
fi
if [[ ! $hasSystemd ]]; then
echo "extra-container requires systemd."
exit 1
fi
if [[ ! -e /nix/var/nix/profiles/default ]]; then
echo "extra-container requires a multi-user nix installation."
exit 1
fi
## 1. Build extra-container
tmpDir=$(mktemp -d)
trap "rm -rf $tmpDir" EXIT
nix-build --out-link $tmpDir/extra-container -E "(import <nixpkgs> {}).callPackage ''$scriptDir/..'' {}"
## 2. Install to root user profile
sudo $(type -P nix-env) -i $tmpDir/extra-container
## 3. Edit /etc/sudoers to enable running extra-container via sudo
# See ./edit-sudoers.rb for more details
if ! type -P ruby > /dev/null; then
nix-build --out-link $tmpDir/ruby '<nixpkgs>' -A ruby > /dev/null
export PATH="$tmpDir/ruby/bin${PATH:+:}$PATH"
fi
extraSecurePaths=/root/.nix-profile/bin:/nix/var/nix/profiles/default/bin
newSudoersContent=$(sudo cat /etc/sudoers | ruby "$scriptDir"/generate-sudoers.rb "$extraSecurePaths")
if [[ $newSudoersContent ]]; then
echo "$newSudoersContent" | sudo EDITOR="tee" visudo >/dev/null
fi