-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_setup.py
More file actions
125 lines (98 loc) · 3.11 KB
/
env_setup.py
File metadata and controls
125 lines (98 loc) · 3.11 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
import subprocess
import getpass
import shutil
import sys
import os
from pathlib import Path
def prerequisites():
commands = [
["sudo", "apt", "update"],
["sudo", "apt", "upgrade", "-y"],
["sudo", "apt", "install", "-y",
"gh", "git", "curl",
"openjdk-17-jdk", "openjdk-17-jre",
"nix", "screen", "cmake"]
]
for cmd in commands:
print(f"Running {' '.join(cmd)}")
result = subprocess.run(cmd)
if result.returncode != 0:
print("Command failed:", cmd)
return -1;
else:
print("\n\nPackages installed\n\n")
return 0;
def lftoolchain():
curl = subprocess.Popen(
["curl", "-Ls", "https://install.lf-lang.org"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
try:
subprocess.run(
["bash", "-s", "nightly", "cli"],
stdin=curl.stdout,
check=True
)
except subprocess.CalledProcessError as e:
print("Installation failed")
return -1;
print("\n\nLF toolchain installation successful\n\n")
return 0
def nixsetup():
user = getpass.getuser()
nix_dir = Path.home()/".config"/"nix"
nix_file = nix_dir/"nix.conf"
commands = [
["groups"],
["sudo", "usermod", "-aG", "nix-users", user]
]
cmd = commands[0]
result = subprocess.run(cmd, capture_output=True, text=True, check = True)
if "nix-users" in result.stdout:
print("\n\nNix users are added\n\n")
#return 0
else:
try:
subprocess.run(commands[1], check=True,
capture_output=True,
text=True
)
except subprocess.CalledProcessError as e:
print("\n\n Failed to set up nix\n\n")
print("Code:", e.returncode)
return -1
print("\n\nAdded nix to groups. Reboot needed!!!\n\n")
#return 0
nix_dir.mkdir(parents=True, exist_ok=True)
line = "experimental-features = nix-command flakes\n"
if nix_file.exists():
read_lines_nix = nix_file.read_text()
if line.strip() in read_lines_nix:
print("\n\nNix experimental-features are already enabled")
else:
try:
with nix_file.open("a") as f:
f.write(line)
print("\n\n Nix configured, reboot if needed.\n\n")
except PermissionError:
print("Permission error with writing")
except FileNotFoundError:
print("Unable to locate file")
except IsADirectoryError:
print("That is a directory and not a file!!")
except OSError as e:
print(str(e))
return -1
return 0
def main():
if prerequisites() != 0:
sys.exit(1)
if lftoolchain() != 0:
sys.exit(1)
if nixsetup() != 0:
sys.exit(1)
print("\n\n All prerequisites are installed and packages are ready.\n\n")
if __name__ == "__main__":
main()