nixos/captive-browser: make it work without a fixed interface

nixpkgs-fmt makes this a lot noisier than it has to be
This commit is contained in:
Peter Hoeg 2020-10-02 12:50:27 +08:00
parent 21a6759463
commit 04e9e9e510

View File

@ -1,7 +1,6 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib; with lib;
let let
cfg = config.programs.captive-browser; cfg = config.programs.captive-browser;
in in
@ -27,15 +26,17 @@ in
# the options below are the same as in "captive-browser.toml" # the options below are the same as in "captive-browser.toml"
browser = mkOption { browser = mkOption {
type = types.str; type = types.str;
default = concatStringsSep " " [ "${pkgs.chromium}/bin/chromium" default = concatStringsSep " " [
"--user-data-dir=\${XDG_DATA_HOME:-$HOME/.local/share}/chromium-captive" ''${pkgs.chromium}/bin/chromium''
''--proxy-server="socks5://$PROXY"'' ''--user-data-dir=''${XDG_DATA_HOME:-$HOME/.local/share}/chromium-captive''
''--host-resolver-rules="MAP * ~NOTFOUND , EXCLUDE localhost"'' ''--proxy-server="socks5://$PROXY"''
"--no-first-run" ''--host-resolver-rules="MAP * ~NOTFOUND , EXCLUDE localhost"''
"--new-window" ''--no-first-run''
"--incognito" ''--new-window''
"http://cache.nixos.org/" ''--incognito''
]; ''-no-default-browser-check''
''http://cache.nixos.org/''
];
description = '' description = ''
The shell (/bin/sh) command executed once the proxy starts. The shell (/bin/sh) command executed once the proxy starts.
When browser exits, the proxy exits. An extra env var PROXY is available. When browser exits, the proxy exits. An extra env var PROXY is available.
@ -81,42 +82,45 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
programs.captive-browser.dhcp-dns = mkOptionDefault ( programs.captive-browser.dhcp-dns =
if config.networking.networkmanager.enable then let
"${pkgs.networkmanager}/bin/nmcli dev show ${escapeShellArg cfg.interface} | ${pkgs.gnugrep}/bin/fgrep IP4.DNS" iface = prefix:
else if config.networking.dhcpcd.enable then optionalString cfg.bindInterface (concatStringsSep " " (map escapeShellArg [ prefix cfg.interface ]));
"${pkgs.dhcpcd}/bin/dhcpcd -U ${escapeShellArg cfg.interface} | ${pkgs.gnugrep}/bin/fgrep domain_name_servers" in
else if config.networking.useNetworkd then mkOptionDefault (
"${cfg.package}/bin/systemd-networkd-dns ${escapeShellArg cfg.interface}" if config.networking.networkmanager.enable then
else "${pkgs.networkmanager}/bin/nmcli dev show ${iface ""} | ${pkgs.gnugrep}/bin/fgrep IP4.DNS"
"${config.security.wrapperDir}/udhcpc --quit --now -f -i ${escapeShellArg cfg.interface} -O dns --script ${ else if config.networking.dhcpcd.enable then
pkgs.writeScript "udhcp-script" '' "${pkgs.dhcpcd}/bin/dhcpcd ${iface "-U"} | ${pkgs.gnugrep}/bin/fgrep domain_name_servers"
#!/bin/sh else if config.networking.useNetworkd then
if [ "$1" = bound ]; then "${cfg.package}/bin/systemd-networkd-dns ${iface ""}"
echo "$dns" else
fi "${config.security.wrapperDir}/udhcpc --quit --now -f ${iface "-i"} -O dns --script ${
''}" pkgs.writeShellScript "udhcp-script" ''
); if [ "$1" = bound ]; then
echo "$dns"
fi
''}"
);
security.wrappers.udhcpc = { security.wrappers.udhcpc = {
capabilities = "cap_net_raw+p"; capabilities = "cap_net_raw+p";
source = "${pkgs.busybox}/bin/udhcpc"; source = "${pkgs.busybox}/bin/udhcpc";
}; };
security.wrappers.captive-browser = { security.wrappers.captive-browser = {
capabilities = "cap_net_raw+p"; capabilities = "cap_net_raw+p";
source = pkgs.writeScript "captive-browser" '' source = pkgs.writeShellScript "captive-browser" ''
#!${pkgs.bash}/bin/bash export XDG_CONFIG_HOME=${pkgs.writeTextDir "captive-browser.toml" ''
export XDG_CONFIG_HOME=${pkgs.writeTextDir "captive-browser.toml" '' browser = """${cfg.browser}"""
browser = """${cfg.browser}""" dhcp-dns = """${cfg.dhcp-dns}"""
dhcp-dns = """${cfg.dhcp-dns}""" socks5-addr = """${cfg.socks5-addr}"""
socks5-addr = """${cfg.socks5-addr}""" ${optionalString cfg.bindInterface ''
${optionalString cfg.bindInterface '' bind-device = """${cfg.interface}"""
bind-device = """${cfg.interface}""" ''}
''} ''}
''} exec ${cfg.package}/bin/captive-browser
exec ${cfg.package}/bin/captive-browser '';
'';
}; };
}; };
} }