]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - nixops/scripts/setup
Add sandbox warning in setup script
[perso/Immae/Config/Nix.git] / nixops / scripts / setup
CommitLineData
9f5da6d7
IB
1#!/bin/bash
2
05ec8138
IB
3set -euxo pipefail
4
bcd108fe 5RemoteRepo="gitolite@git.immae.eu:perso/Immae/Prive/Password_store/Sites"
568d4240
IB
6DeploymentUuid="cef694f3-081d-11e9-b31f-0242ec186adf"
7
8if ! which nix 2>/dev/null >/dev/null; then
9 cat <<-EOF
10 nix is needed, please install it:
11 > curl https://nixos.org/nix/install | sh
12 (or any other way handled by your distribution)
13 EOF
14 exit 1
15fi
9f5da6d7 16
df6dc085
IB
17if [ "${NIX_STORE:-/nix/store}" != "/nix/store" ]; then
18 cat <<-EOF
19 Nix store outside of /nix/store is not supported
20 EOF
21 exit 1
22fi
23
9f5da6d7
IB
24if [ -z "$NIXOPS_CONFIG_PASS_SUBTREE_REMOTE" \
25 -o -z "$NIXOPS_CONFIG_PASS_SUBTREE_PATH" ]; then
26 cat <<-EOF
568d4240
IB
27 Two environment variables are needed to setup the password store:
28 NIXOPS_CONFIG_PASS_SUBTREE_PATH : path where the subtree will be imported
29 NIXOPS_CONFIG_PASS_SUBTREE_REMOTE : remote name to give to the repository
30 EOF
9f5da6d7
IB
31 exit 1
32fi
33
34if ! pass $NIXOPS_CONFIG_PASS_SUBTREE_PATH > /dev/null 2>/dev/null; then
35 cat <<-EOF
568d4240
IB
36 /!\ This will modify your password store to add and import a subtree
37 with the specific passwords files. Choose a path that doesn’t exist
38 yet in your password store.
39 > pass git remote add $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE $RemoteRepo
40 > pass git subtree add --prefix=$NIXOPS_CONFIG_PASS_SUBTREE_PATH $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE master
41 Later, you can use pull_environment and push_environment scripts to
42 update the passwords when needed
43 Continue? [y/N]
44 EOF
9f5da6d7
IB
45 read y
46 if [ "$y" = "y" -o "$y" = "Y" ]; then
47 pass git remote add $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE $RemoteRepo
48 pass git subtree add --prefix=$NIXOPS_CONFIG_PASS_SUBTREE_PATH $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE master
49 else
50 echo "Aborting"
51 exit 1
52 fi
53fi
54
df6dc085
IB
55nix_group=$(stat -c %G /nix/store)
56if [ "$nix_group" = "nixbld" ]; then
57 nix_user="nixbld1"
58else
59 nix_user="$(stat -c %U /nix/store)"
60fi
61
9f5da6d7 62if [ ! -f /etc/ssh/ssh_rsa_key_nixops ]; then
568d4240
IB
63 cat <<-EOF
64 The key to access private git repositories (websites hosted by the
65 server) needs to be accessible to nix builders. It will be put in
66 /etc/ssh/ssh_rsa_key_nixops (sudo right is needed for that)
9690acd9
IB
67 > pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey | sudo tee /etc/ssh/ssh_rsa_key_nixops > /dev/null
68 > pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey.pub | sudo tee /etc/ssh/ssh_rsa_key_nixops.pub > /dev/null
568d4240 69 > sudo chmod u=r,go-rwx /etc/ssh/ssh_rsa_key_nixops
df6dc085 70 > sudo chown $nix_user:$nix_group /etc/ssh/ssh_rsa_key_nixops /etc/ssh/ssh_rsa_key_nixops.pub
568d4240
IB
71 Continue? [y/N]
72 EOF
9f5da6d7
IB
73 read y
74 if [ "$y" = "y" -o "$y" = "Y" ]; then
df6dc085
IB
75 if ! id -u $nix_user 2>/dev/null >/dev/null; then
76 echo "User $nix_user seems inexistant, did you install nix?"
9f5da6d7
IB
77 exit 1
78 fi
79 mask=$(umask)
80 umask 0777
81 # Don’t forward it directly to tee, it would break ncurse pinentry
9690acd9 82 key=$(pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey)
9f5da6d7
IB
83 echo "$key" | sudo tee /etc/ssh/ssh_rsa_key_nixops > /dev/null
84 sudo chmod u=r,go=- /etc/ssh/ssh_rsa_key_nixops
9690acd9 85 pubkey=$(pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey.pub)
9f5da6d7
IB
86 echo "$pubkey" | sudo tee /etc/ssh/ssh_rsa_key_nixops.pub > /dev/null
87 sudo chmod a=r /etc/ssh/ssh_rsa_key_nixops.pub
df6dc085 88 sudo chown $nix_user:$nix_group /etc/ssh/ssh_rsa_key_nixops /etc/ssh/ssh_rsa_key_nixops.pub
9f5da6d7
IB
89 umask $mask
90 else
91 echo "Aborting"
92 exit 1
93 fi
94fi
95
08822d6f
IB
96if nix show-config --json | jq -e '.sandbox.value == "true"' >/dev/null; then
97 cat <<-EOF
98 There are some impure derivations in the repo currently (grep __noChroot), please put
99 sandbox = "relaxed"
100 in /etc/nix/nix.conf
101 you may also want to add
102 keep-outputs = true
103 keep-derivations = true
104 to prevent garbage collector from deleting build dependencies (they take a lot of time to build)
105 EOF
106 exit 1
107fi
108
568d4240
IB
109if ! which nixops 2>/dev/null >/dev/null; then
110 cat <<-EOF
111 nixops is needed:
112 > nix-env -i nixops
113 If it fails, please check that $HOME/.nix-profile/bin is in your PATH.
114 Continue? [y/N]
115 EOF
116 read y
117 if [ "$y" = "y" -o "$y" = "Y" ]; then
118 nix-env -i nixops
119 if ! which nixops 2>/dev/null >/dev/null; then
120 echo "Installation failed, please check that $HOME/.nix-profile/bin is in your path."
121 exit 1
122 fi
123 else
124 echo "Aborting"
125 exit 1
126 fi
127fi
128
129DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
130export NIXOPS_STATE="$(dirname $DIR)/state/eldiron.nixops"
131export NIXOPS_DEPLOYMENT="$DeploymentUuid"
132
133if ! nixops info 2>/dev/null >/dev/null; then
134 cat <<-EOF
135 Importing deployment file into nixops:
136 Continue? [y/N]
137 EOF
138 read y
139 if [ "$y" = "y" -o "$y" = "Y" ]; then
9690acd9 140 deployment=$(pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/Deployment)
568d4240
IB
141 echo "$deployment" | nixops import
142
143 nixops modify "$(dirname $DIR)/eldiron.nix"
144 else
145 echo "Aborting"
146 exit 1
34c58714 147 fi
9f5da6d7 148fi
34c58714 149
568d4240
IB
150cat <<-EOF
151 All set up.
152 Please make sure you’re using scripts/nixops_wrap when deploying
153 EOF