mirror of
https://github.com/ryan4yin/nix-config.git
synced 2026-07-12 07:32:40 +02:00
refactor: scripts
This commit is contained in:
+35
-18
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
https://github.com/NixOS/nix/issues/1472#issuecomment-1532955973
|
https://github.com/NixOS/nix/issues/1472#issuecomment-1532955973
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import plistlib
|
import plistlib
|
||||||
import shlex
|
import shlex
|
||||||
@@ -14,29 +15,45 @@ from pathlib import Path
|
|||||||
NIX_DAEMON_PLIST = Path("/Library/LaunchDaemons/org.nixos.nix-daemon.plist")
|
NIX_DAEMON_PLIST = Path("/Library/LaunchDaemons/org.nixos.nix-daemon.plist")
|
||||||
NIX_DAEMON_NAME = "org.nixos.nix-daemon"
|
NIX_DAEMON_NAME = "org.nixos.nix-daemon"
|
||||||
# http proxy provided by my homelab's bypass router
|
# http proxy provided by my homelab's bypass router
|
||||||
HTTP_PROXY = "http://192.168.5.103:7890"
|
HTTP_PROXY = "http://192.168.5.179:7890"
|
||||||
|
|
||||||
pl = plistlib.loads(NIX_DAEMON_PLIST.read_bytes())
|
PLIST = plistlib.loads(NIX_DAEMON_PLIST.read_bytes())
|
||||||
|
|
||||||
# set http/https proxy
|
|
||||||
# NOTE: curl only accept the lowercase of `http_proxy`!
|
|
||||||
# NOTE: https://curl.se/libcurl/c/libcurl-env.html
|
|
||||||
# pl["EnvironmentVariables"]["http_proxy"] = HTTP_PROXY
|
|
||||||
# pl["EnvironmentVariables"]["https_proxy"] = HTTP_PROXY
|
|
||||||
|
|
||||||
# remove http proxy
|
def update_plist():
|
||||||
pl["EnvironmentVariables"].pop("http_proxy", None)
|
os.chmod(NIX_DAEMON_PLIST, 0o644)
|
||||||
pl["EnvironmentVariables"].pop("https_proxy", None)
|
NIX_DAEMON_PLIST.write_bytes(plistlib.dumps(PLIST))
|
||||||
|
os.chmod(NIX_DAEMON_PLIST, 0o444)
|
||||||
|
|
||||||
os.chmod(NIX_DAEMON_PLIST, 0o644)
|
|
||||||
NIX_DAEMON_PLIST.write_bytes(plistlib.dumps(pl))
|
|
||||||
os.chmod(NIX_DAEMON_PLIST, 0o444)
|
|
||||||
|
|
||||||
# reload the plist
|
def reload_daemon():
|
||||||
for cmd in (
|
# reload the plist
|
||||||
|
for cmd in (
|
||||||
f"launchctl unload {NIX_DAEMON_PLIST}",
|
f"launchctl unload {NIX_DAEMON_PLIST}",
|
||||||
f"launchctl load {NIX_DAEMON_PLIST}",
|
f"launchctl load {NIX_DAEMON_PLIST}",
|
||||||
):
|
):
|
||||||
print(cmd)
|
print(cmd)
|
||||||
subprocess.run(shlex.split(cmd), capture_output=False)
|
subprocess.run(shlex.split(cmd), capture_output=False)
|
||||||
|
|
||||||
|
|
||||||
|
def set_proxy():
|
||||||
|
# set http/https proxy
|
||||||
|
# NOTE: curl only accept the lowercase of `http_proxy`!
|
||||||
|
# NOTE: https://curl.se/libcurl/c/libcurl-env.html
|
||||||
|
PLIST["EnvironmentVariables"]["http_proxy"] = HTTP_PROXY
|
||||||
|
PLIST["EnvironmentVariables"]["https_proxy"] = HTTP_PROXY
|
||||||
|
update_plist()
|
||||||
|
reload_daemon()
|
||||||
|
|
||||||
|
|
||||||
|
def unset_proxy():
|
||||||
|
# remove http proxy
|
||||||
|
PLIST["EnvironmentVariables"].pop("http_proxy", None)
|
||||||
|
PLIST["EnvironmentVariables"].pop("https_proxy", None)
|
||||||
|
update_plist()
|
||||||
|
reload_daemon()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
set_proxy()
|
||||||
|
# unset_proxy()
|
||||||
|
|||||||
Reference in New Issue
Block a user