-
Notifications
You must be signed in to change notification settings - Fork 6
/
nm2nix.py
30 lines (23 loc) · 948 Bytes
/
nm2nix.py
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
from os import listdir, getpid
from subprocess import check_output
from os.path import isfile, join
import configparser
import tempfile
import json
path = "./"
files = [f for f in listdir(path) if isfile(join(path, f))]
nmfiles = [f for f in files if f.endswith(".nmconnection")]
jsonConfigs = {}
for i in nmfiles:
config = configparser.ConfigParser(delimiters=('=', ))
config.read(i)
connection_name = i.removesuffix(".nmconnection")
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]
tf = tempfile.TemporaryFile("w+")
jsonConfigs = json.dump(jsonConfigs, tf)
tf.flush()
print(check_output(["nix-instantiate", "--expr", "--eval", f"builtins.fromJSON (builtins.readFile \"/proc/{getpid()}/fd/{tf.fileno()}\")"], text=True)) # noqa: E501