summarylogtreecommitdiffstats
path: root/nix.install
blob: 613cee6d0413c3ee55cad0dfb44c100f81bfb99d (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

pre_install () {
  # Check that the group and users don't already exist
  if [[ -n $(cut -d: -f1 /etc/group | grep -w nixbld) ]]; then
    echo "The nixbld group already exists.  This install cannot proceed."
    exit 1
  fi

  for i in {1..10}; do
    if [[ -n $(cut -d: -f1 /etc/passwd | grep -w nixbld$i) ]]; then
      echo "The nixbld$i user already exists.  This install cannot proceed."
      exit 1
    fi
  done
}

create_users () {
  # Create a nixbld group.
  groupadd -r nixbld

  # Create 10 nixbld{i} users
  for i in {1..10}; do
    useradd -g nixbld -G nixbld -r -N -M -d /var/empty -s /sbin/nologin nixbld$i
  done
}

delete_users() {
  # Remove the users
  for i in {1..10}; do
    userdel nixbld$i
  done

  # Remove the group
  groupdel nixbld
}

create_store() {
  # Create nix folders and set permissions
  mkdir -p /nix/store
  chown root.nixbld /nix/store
  chmod 1775 /nix/store
  mkdir -p -m 1777 /nix/var/nix/gcroots/per-user
  mkdir -p -m 1777 /nix/var/nix/profiles/per-user
}

restore_store() {
  # Restore folder permissions
  chmod 755 /nix/store
  chown root.root /nix/store
}

init_channels() {
 # Initialize default nix channel
  echo "Initializing default nix channel"
  source /etc/profile.d/nix.sh
  nix-channel --update
}

daemon_info() {
  echo "To start the nix daemon, execute one of the following:"
  echo
  echo "    systemctl enable nix-daemon.socket       # Sets the daemon to start on next boot"
  echo "    systemctl enable --now nix-daemon.socket # Starts now and on next boot too"
  echo
  echo "Also, if this is a new install, you need to start a new login shell or otherwise"
  echo
  echo "    source /etc/profile.d/nix.sh"
  echo "    source /etc/profile.d/nix-daemon.sh"
  echo
  echo "Once your environment is set-up, you will need to add some channels.  You can see how"
  echo "to do that here:"
  echo "    https://nixos.org/nix/manual/#sec-channels"
}

post_install() {
  create_users
  create_store
  init_channels
  daemon_info
}

pre_upgrade() {
  systemctl stop nix-daemon.socket
  systemctl stop nix-daemon
}

post_upgrade() {
  delete_users
  create_users
  create_store
  daemon_info
}

pre_remove() {
  restore_store
  delete_users
}