-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnm2nix.py
More file actions
90 lines (77 loc) · 2.47 KB
/
nm2nix.py
File metadata and controls
90 lines (77 loc) · 2.47 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
from subprocess import check_output
from os.path import isfile
import configparser
import tempfile
import json
import argparse
from pathlib import Path
def json_to_nix(input) -> str:
output = ""
with tempfile.NamedTemporaryFile(mode="w") as tf:
tf.write(json.dumps(input))
tf.flush()
output = check_output(
[
"nix-instantiate",
"--expr",
"--eval",
f'builtins.fromJSON (builtins.readFile "{tf.name}")',
],
text=True,
)
return output
PATHS = [
"/etc/NetworkManager/system-connections",
]
parser = argparse.ArgumentParser(
prog="nm2nix",
description="Converts .nmconnection files into nix code",
)
parser.add_argument(
"-path",
help=f"The Path in which to search for .nmconnection files \n supply multiple times to search in multiple paths \n defaults to: { ", ".join(PATHS)}",
action="append",
)
parser.add_argument(
"-s", help="wether to output one file per connection", action="store_true"
)
TARGET_DEFAULT = "network_connections/"
parser.add_argument(
"-target",
help=f"path under which to save the generated files \n default to: {TARGET_DEFAULT}", default=TARGET_DEFAULT
)
parser.add_argument(
"-overwrite", help="wether to overwrite existing files", action="store_true"
)
args = parser.parse_args()
paths = []
if args.path is None:
paths = PATHS
else:
paths = args.path
files = []
for path in paths:
files += list(filter(isfile,Path(path).glob("*.nmconnection")))
jsonConfigs = {}
for i in files:
config = configparser.ConfigParser(delimiters=('=', ), interpolation=None)
config.read(i)
connection_name = i.stem
jsonConfigs[connection_name] = {}
for section in config.sections():
jsonConfigs[connection_name][section] = {}
for key in config[section]:
jsonConfigs[connection_name][section][key] = config[section][key]
if not args.s:
print(json_to_nix(jsonConfigs))
else:
target_dir = Path(args.target)
if len(jsonConfigs) != 0 and not target_dir.exists():
target_dir.mkdir()
for key, value in jsonConfigs.items():
target_path = target_dir / (key + ".nix")
if target_path.exists() and not args.overwrite:
print(f"skipping writing to {target_path} because it already exists, use -overwrite to overwrite existing files")
continue
with target_path.open("w") as f:
f.write(json_to_nix(value))