Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f3a25fa14 | ||
|
|
0c06237cac | ||
|
|
2d6a7329ac | ||
|
|
02e738d0b4 | ||
|
|
9bcb89edac | ||
|
|
20fb312d40 |
No files matched your search
Generated
+25
-25
@@ -7,11 +7,11 @@
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1781152676,
|
||||
"narHash": "sha256-RxWs5ND31KzTG7wvMM+PMfUjyNpmIEr999lqNARaM5o=",
|
||||
"lastModified": 1792526520,
|
||||
"narHash": "sha256-3hSJlv8mUVnZPMWK2+CM7a9O+nR6Z4mCh27NfLCmTPc=",
|
||||
"owner": "nix-community",
|
||||
"repo": "disko",
|
||||
"rev": "ff8702b4de27f72b4c78573dfb89ec74e36abdf1",
|
||||
"rev": "cedd626e11b635470fa6f9fdb8ad79b0075dd7f9",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -27,11 +27,11 @@
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1788355065,
|
||||
"narHash": "sha256-gAz5oTI7ur34CfuakQduiQd/2ZhO62Bn2XXg08nZYYQ=",
|
||||
"lastModified": 1794252721,
|
||||
"narHash": "sha256-sD1rhEjMghSyWSUG2xEOw+FOVd2wnoWwi/809w8fRkI=",
|
||||
"owner": "nix-community",
|
||||
"repo": "home-manager",
|
||||
"rev": "d9d750e4fc11c10cab2da677bdd31e427f3a3a71",
|
||||
"rev": "f1db7c474bb49babfe9d8def532939e0c7404327",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -67,11 +67,11 @@
|
||||
"nixpkgs": "nixpkgs"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1769548169,
|
||||
"narHash": "sha256-03+JxvzmfwRu+5JafM0DLbxgHttOQZkUtDWBmeUkN8Y=",
|
||||
"lastModified": 1792739756,
|
||||
"narHash": "sha256-s0TeXWG5Ci75Y/Hzon0Z7NrN/fG773Rc54/z6oPFszQ=",
|
||||
"owner": "nix-community",
|
||||
"repo": "impermanence",
|
||||
"rev": "7b1d382faf603b6d264f58627330f9faa5cba149",
|
||||
"rev": "fa627f4c8b8e92f58f58a73d99ee4e1304e64eb2",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -85,11 +85,11 @@
|
||||
"nixpkgs": "nixpkgs_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1791695179,
|
||||
"narHash": "sha256-m4AV3cIczSMgWJpF85UhqCea5FilfvwrIJm5UwLZbmA=",
|
||||
"lastModified": 1794202607,
|
||||
"narHash": "sha256-foUWdIx5HTEopoOHXzTy7uhbZYYl1BdjZKPQpkR+pgg=",
|
||||
"owner": "nix-community",
|
||||
"repo": "nix-vscode-extensions",
|
||||
"rev": "f91eac91132b84a439a04ff983aabafa039bedd6",
|
||||
"rev": "934620178cf8eed2058484c24b5db62dbf4f08b1",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -116,11 +116,11 @@
|
||||
},
|
||||
"nixpkgs-stable": {
|
||||
"locked": {
|
||||
"lastModified": 1788297115,
|
||||
"narHash": "sha256-Z+vUNbfd2FIKkWOTkcT7RYlh3oFCnig/d2eXD1SWf2E=",
|
||||
"lastModified": 1793865203,
|
||||
"narHash": "sha256-/cpdPHaySDIyCgZI6EWngiM6jK3YLUNzv1b7lTzy6Bk=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "a3116115851d68b8952a2a4221cc25a84e56b532",
|
||||
"rev": "c56a87d8e41ce69bd2af57eb35dcc117a9dfca33",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -148,11 +148,11 @@
|
||||
},
|
||||
"nixpkgs_3": {
|
||||
"locked": {
|
||||
"lastModified": 1789785513,
|
||||
"narHash": "sha256-B44WL6h0XoLjJ41bUPJk0X5SDinLCII//6EcBLXKiJ0=",
|
||||
"lastModified": 1794042290,
|
||||
"narHash": "sha256-ziZn2PvCJE5JEEpqTdxXwJpPB+L3YqoCpTAMCzyaP3U=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "20b1ddd1aa5ace70c9468305030aa4f9ef79671b",
|
||||
"rev": "f4a03c008967841f2a0b3cb7c436fc6fc31671f6",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -176,11 +176,11 @@
|
||||
},
|
||||
"secrets": {
|
||||
"locked": {
|
||||
"lastModified": 1793195490,
|
||||
"narHash": "sha256-1ReF2oGuTGF1osuK3Pc0N4C5QKQfoSbv/2HO3OJCkqw=",
|
||||
"lastModified": 1795000895,
|
||||
"narHash": "sha256-DzTRUpiuhSmPJm/MjcUwNLxVbhWxjC1TGLyj41kXnr4=",
|
||||
"ref": "refs/heads/main",
|
||||
"rev": "02313098cbcf69356df75ba0895c913b053a2edf",
|
||||
"revCount": 22,
|
||||
"rev": "557ab40690eb4320e36c5e57b73bfba1ecb73393",
|
||||
"revCount": 23,
|
||||
"type": "git",
|
||||
"url": "ssh://gitea@git.bulthuis.dev/Jan/nixos-secrets"
|
||||
},
|
||||
@@ -196,11 +196,11 @@
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1789890976,
|
||||
"narHash": "sha256-GKwH3zpy7tartuJMG0Rv/xUsdetG1QLmTVv8UKgJLmA=",
|
||||
"lastModified": 1793995029,
|
||||
"narHash": "sha256-ZlE4BtMjWhfC6rFXLO9Qimn36Ene1INqCgKDDvazjhc=",
|
||||
"owner": "Mic92",
|
||||
"repo": "sops-nix",
|
||||
"rev": "7214124c20c1542c90deb54af50e2f53ae02711f",
|
||||
"rev": "89a234ce2daf6e568a7d4b4fe250afdd23d84e20",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
||||
@@ -93,5 +93,8 @@
|
||||
};
|
||||
|
||||
# Set up hardware
|
||||
imports = [ ./hardware-configuration.nix ];
|
||||
imports = [
|
||||
./hardware-configuration.nix
|
||||
./seedbox.nix
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
{
|
||||
config,
|
||||
inputs,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
|
||||
# Always-on torrent client, confined to a VPN-only network namespace, plus a
|
||||
# read-only, password-protected WebDAV share of its downloads for the local network.
|
||||
#
|
||||
# Everything provider-specific lives in secrets:
|
||||
# seedbox/wireguard wg-quick style config (keys, peer, Address, DNS, MTU)
|
||||
# seedbox/port the incoming port forwarded by the VPN
|
||||
# seedbox/share-password password for the share (user "seedbox")
|
||||
#
|
||||
# Web interface: only on this host's localhost, e.g. `ssh -L 8081:127.0.0.1:8081 ws-home`
|
||||
let
|
||||
netns = "seedbox";
|
||||
wgIf = "wg-seedbox";
|
||||
dataDir = "/var/lib/seedbox";
|
||||
downloadsDir = "${dataDir}/downloads";
|
||||
webuiProxyPort = 8081; # localhost of this host only
|
||||
webuiPort = 8080; # inside the namespace
|
||||
sharePort = 8091;
|
||||
|
||||
secret = name: config.sops.secrets."seedbox/${name}".path;
|
||||
secretFile = "${inputs.secrets}/secrets/ws-home.enc.yaml";
|
||||
|
||||
user = config.services.qbittorrent.user;
|
||||
group = config.services.qbittorrent.group;
|
||||
inNetns = {
|
||||
NetworkNamespacePath = "/run/netns/${netns}";
|
||||
};
|
||||
netnsDeps = {
|
||||
bindsTo = [ "seedbox-netns.service" ];
|
||||
after = [ "seedbox-netns.service" ];
|
||||
};
|
||||
in
|
||||
{
|
||||
sops.secrets = {
|
||||
"seedbox/wireguard".sopsFile = secretFile;
|
||||
"seedbox/port" = {
|
||||
sopsFile = secretFile;
|
||||
owner = user;
|
||||
};
|
||||
"seedbox/share-password" = {
|
||||
sopsFile = secretFile;
|
||||
owner = user;
|
||||
};
|
||||
};
|
||||
|
||||
modules.impermanence.directories = [
|
||||
{
|
||||
directory = dataDir;
|
||||
inherit user group;
|
||||
mode = "0750";
|
||||
}
|
||||
];
|
||||
systemd.tmpfiles.rules = [ "d ${downloadsDir} 0750 ${user} ${group} -" ];
|
||||
|
||||
# Network namespace whose only route out is the WireGuard interface. The interface
|
||||
# is created here and moved into the namespace, so its encrypted traffic uses the
|
||||
# host's network, but nothing inside the namespace can reach anything else.
|
||||
systemd.services.seedbox-netns = {
|
||||
description = "Network namespace for the seedbox";
|
||||
wants = [ "network-online.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
path = with pkgs; [
|
||||
iproute2
|
||||
wireguard-tools
|
||||
gnugrep
|
||||
gnused
|
||||
coreutils
|
||||
];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
RuntimeDirectory = "seedbox";
|
||||
RuntimeDirectoryPreserve = true;
|
||||
};
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
conf=${secret "wireguard"}
|
||||
field() { grep -iE "^\s*$1\s*=" "$conf" | cut -d= -f2- | tr ',' '\n' | sed 's/ //g'; }
|
||||
|
||||
ip netns del ${netns} 2>/dev/null || true
|
||||
ip link del ${wgIf} 2>/dev/null || true
|
||||
|
||||
ip netns add ${netns}
|
||||
ip -n ${netns} link set lo up
|
||||
|
||||
ip link add ${wgIf} type wireguard
|
||||
wg setconf ${wgIf} <(wg-quick strip "$conf")
|
||||
ip link set ${wgIf} netns ${netns}
|
||||
for addr in $(field Address); do
|
||||
ip -n ${netns} addr add "$addr" dev ${wgIf}
|
||||
done
|
||||
mtu=$(field MTU | head -1)
|
||||
ip -n ${netns} link set ${wgIf} mtu "''${mtu:-1420}" up
|
||||
ip -n ${netns} route add default dev ${wgIf}
|
||||
if field Address | grep -q ':'; then
|
||||
ip -n ${netns} -6 route add default dev ${wgIf}
|
||||
fi
|
||||
|
||||
# DNS only through the tunnel
|
||||
field DNS | grep -v ':' | sed 's/^/nameserver /' > /run/seedbox/resolv.conf
|
||||
echo 'hosts: files dns' > /run/seedbox/nsswitch.conf
|
||||
'';
|
||||
preStop = ''
|
||||
ip netns del ${netns} || true
|
||||
'';
|
||||
};
|
||||
|
||||
services.qbittorrent = {
|
||||
enable = true;
|
||||
profileDir = dataDir;
|
||||
inherit webuiPort;
|
||||
extraArgs = [ "--confirm-legal-notice" ];
|
||||
};
|
||||
|
||||
systemd.services.qbittorrent = netnsDeps // {
|
||||
serviceConfig = inNetns // {
|
||||
# Resolve names only with the tunnel's DNS. The host's nscd would resolve them
|
||||
# outside the namespace, so it is hidden.
|
||||
BindReadOnlyPaths = [
|
||||
"/run/seedbox/resolv.conf:/etc/resolv.conf"
|
||||
"/run/seedbox/nsswitch.conf:/etc/nsswitch.conf"
|
||||
];
|
||||
InaccessiblePaths = [ "-/run/nscd" ];
|
||||
ReadWritePaths = [ dataDir ];
|
||||
|
||||
# Initial settings, and the incoming port from the secret on every start. Other
|
||||
# settings are left to the web interface.
|
||||
ExecStartPre = pkgs.writeShellScript "seedbox-config" ''
|
||||
set -euo pipefail
|
||||
PATH=${
|
||||
lib.makeBinPath (
|
||||
with pkgs;
|
||||
[
|
||||
coreutils
|
||||
gnused
|
||||
]
|
||||
)
|
||||
}
|
||||
port=$(tr -dc 0-9 < ${secret "port"})
|
||||
cfg=${dataDir}/qBittorrent/config/qBittorrent.conf
|
||||
mkdir -p ${downloadsDir} "$(dirname "$cfg")"
|
||||
if [ ! -f "$cfg" ]; then
|
||||
cat > "$cfg" <<EOF
|
||||
[BitTorrent]
|
||||
Session\DefaultSavePath=${downloadsDir}
|
||||
Session\Interface=${wgIf}
|
||||
Session\InterfaceName=${wgIf}
|
||||
Session\LSDEnabled=false
|
||||
Session\QueueingSystemEnabled=false
|
||||
Session\Port=$port
|
||||
|
||||
[Network]
|
||||
PortForwardingEnabled=false
|
||||
|
||||
[Preferences]
|
||||
WebUI\Address=127.0.0.1
|
||||
WebUI\Port=${toString webuiPort}
|
||||
EOF
|
||||
else
|
||||
sed -i -E "s|^Session\\\\Port=.*|Session\\\\Port=$port|" "$cfg"
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
# Web interface on this host's localhost, proxied into the namespace
|
||||
systemd.sockets.seedbox-webui = {
|
||||
wantedBy = [ "sockets.target" ];
|
||||
listenStreams = [ "127.0.0.1:${toString webuiProxyPort}" ];
|
||||
};
|
||||
systemd.services.seedbox-webui = netnsDeps // {
|
||||
requires = [ "qbittorrent.service" ];
|
||||
after = [ "qbittorrent.service" ];
|
||||
serviceConfig = inNetns // {
|
||||
ExecStart = "${pkgs.systemd}/lib/systemd/systemd-socket-proxyd 127.0.0.1:${toString webuiPort}";
|
||||
DynamicUser = true;
|
||||
PrivateTmp = true;
|
||||
};
|
||||
};
|
||||
|
||||
# Read-only share of the downloads on the local network, password protected and
|
||||
# not announced anywhere
|
||||
systemd.services.seedbox-share = {
|
||||
description = "Seedbox downloads share";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
serviceConfig = {
|
||||
User = user;
|
||||
Group = group;
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
NoNewPrivileges = true;
|
||||
ReadOnlyPaths = [ dataDir ];
|
||||
};
|
||||
script = ''
|
||||
export RCLONE_USER=seedbox
|
||||
RCLONE_PASS=$(cat ${secret "share-password"})
|
||||
export RCLONE_PASS
|
||||
exec ${lib.getExe pkgs.rclone} serve webdav ${downloadsDir} \
|
||||
--addr :${toString sharePort} --read-only --dir-cache-time 15s
|
||||
'';
|
||||
};
|
||||
networking.firewall.interfaces.eno1.allowedTCPPorts = [ sharePort ];
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
# Forwards a Beat Saber OneClick link to the Steam Frame, where BSManager downloads it,
|
||||
# and keeps a desktop notification up to date until the install is done.
|
||||
# Usage: beatsaber-oneclick <ssh-host> <url>
|
||||
# $remote_script (remote.sh) is set by the package.
|
||||
|
||||
host=${1:?usage: beatsaber-oneclick <ssh-host> <url>}
|
||||
url=${2:?usage: beatsaber-oneclick <ssh-host> <url>}
|
||||
api=https://api.beatsaver.com
|
||||
|
||||
# One notification, updated in place
|
||||
notification_id=
|
||||
notify() {
|
||||
local id
|
||||
if id=$(notify-send --print-id ${notification_id:+--replace-id="$notification_id"} \
|
||||
--app-name="Beat Saber OneClick" --icon=input-gaming "$@"); then
|
||||
notification_id=$id
|
||||
fi
|
||||
}
|
||||
|
||||
# Only forward the OneClick schemes, and only plain URL characters, since the URL
|
||||
# ends up in a remote shell command line.
|
||||
if [[ ! $url =~ ^(beatsaver|bsplaylist|web\+bsmap|modelsaber):// ]] ||
|
||||
[[ ! $url =~ ^[A-Za-z0-9+.:/?#\&=%_~@!,\;-]+$ ]]; then
|
||||
notify --urgency=critical "Not a OneClick link" "$url"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
fetch() {
|
||||
curl -sfL --max-time 20 "$@"
|
||||
}
|
||||
|
||||
# Look up what the link installs, to know which map folders to watch for. If that
|
||||
# fails, the link is still sent, just without tracking.
|
||||
ids=()
|
||||
label=$url
|
||||
playlist=
|
||||
case $url in
|
||||
beatsaver://* | web+bsmap://*)
|
||||
ref=${url#*://}
|
||||
ref=${ref%%[/?#]*}
|
||||
if [[ $url == beatsaver://* ]]; then
|
||||
map=$(fetch "$api/maps/id/$ref") || map=
|
||||
else
|
||||
map=$(fetch "$api/maps/hash/$ref") || map=
|
||||
fi
|
||||
if [[ -n $map ]]; then
|
||||
ids=("$(jq -r '.id | ascii_downcase' <<<"$map")")
|
||||
label=$(jq -r '"\(.metadata.songName) – \(.metadata.levelAuthorName)"' <<<"$map")
|
||||
fi
|
||||
;;
|
||||
bsplaylist://playlist/*)
|
||||
playlist=1
|
||||
label="Playlist"
|
||||
if bplist=$(fetch "${url#bsplaylist://playlist/}"); then
|
||||
label=$(jq -r '.playlistTitle // "Playlist"' <<<"$bplist")
|
||||
# Entries with a key need no lookup; resolve the rest by hash, 50 at a time
|
||||
mapfile -t ids < <(jq -r '.songs[] | .key // empty | ascii_downcase' <<<"$bplist")
|
||||
mapfile -t hashes < <(jq -r '.songs[] | select(.key == null) | .hash // empty | ascii_downcase' <<<"$bplist")
|
||||
for ((i = 0; i < ${#hashes[@]}; i += 50)); do
|
||||
batch=$(
|
||||
IFS=,
|
||||
echo "${hashes[*]:i:50}"
|
||||
)
|
||||
if found=$(fetch "$api/maps/hash/$batch"); then
|
||||
# A single hash returns the map itself instead of a hash → map object
|
||||
mapfile -t -O "${#ids[@]}" ids < <(jq -r 'if has("id") then .id else (.[] | select(. != null) | .id) end | ascii_downcase' <<<"$found")
|
||||
fi
|
||||
done
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
notify "Sending to Steam Frame…" "$label"
|
||||
|
||||
total=${#ids[@]}
|
||||
done_count=0
|
||||
new=0
|
||||
already=0
|
||||
missing=0
|
||||
last_error=
|
||||
stderr_file=$(mktemp)
|
||||
trap 'rm -f "$stderr_file"' EXIT
|
||||
|
||||
exec {events}< <(ssh -T -o BatchMode=yes -o ConnectTimeout=5 "$host" \
|
||||
bash -s -- "$(printf '%q' "$url")" "${ids[@]}" <"$remote_script" 2>"$stderr_file")
|
||||
ssh_pid=$!
|
||||
|
||||
while IFS= read -r -u "$events" event; do
|
||||
case $event in
|
||||
received)
|
||||
notify "Downloading on Steam Frame…" "$label"
|
||||
;;
|
||||
"done "*)
|
||||
done_count=$((done_count + 1))
|
||||
if ((total > 1)); then
|
||||
notify "Downloading on Steam Frame… $done_count/$total" "$label"
|
||||
fi
|
||||
;;
|
||||
"error "*)
|
||||
last_error=${event#error }
|
||||
;;
|
||||
"result "*" new") new=$((new + 1)) ;;
|
||||
"result "*" already") already=$((already + 1)) ;;
|
||||
"result "*" missing") missing=$((missing + 1)) ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if ! wait "$ssh_pid"; then
|
||||
err=$(tr -d '\r' <"$stderr_file" | grep -v 'X11 forwarding' || true)
|
||||
notify --urgency=critical "Couldn't send to Steam Frame" "${err:-ssh failed}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
failure_hint=${last_error:-BSManager didn\'t finish within 2 minutes. Check it on the Frame.}
|
||||
|
||||
if ((total == 0)); then
|
||||
notify "Sent to Steam Frame" "$label"$'\n(Not tracked: couldn\'t look it up on BeatSaver.)'
|
||||
elif [[ -z $playlist ]]; then
|
||||
if ((new > 0)); then
|
||||
notify "Installed on Steam Frame" "$label"
|
||||
elif ((already > 0)); then
|
||||
notify "Already on Steam Frame" "$label"
|
||||
else
|
||||
notify --urgency=critical "Not installed on Steam Frame" "$label"$'\n'"$failure_hint"
|
||||
fi
|
||||
else
|
||||
summary="$label: $new new, $already already there"
|
||||
if ((missing > 0)); then
|
||||
notify --urgency=critical "Playlist partly installed on Steam Frame" \
|
||||
"$summary, $missing failed"$'\n'"$failure_hint"
|
||||
else
|
||||
notify "Playlist installed on Steam Frame" "$summary"
|
||||
fi
|
||||
fi
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
writeShellApplication,
|
||||
openssh,
|
||||
libnotify,
|
||||
curl,
|
||||
jq,
|
||||
}:
|
||||
|
||||
# Forwards Beat Saber OneClick links (BeatSaver, BeatLeader, ScoreSaber, playlists)
|
||||
# to a Steam Frame over SSH and opens them there, so BSManager on the Frame does the
|
||||
# download, and shows the install progress as a notification.
|
||||
# Usage: beatsaber-oneclick <ssh-host> <url>
|
||||
writeShellApplication {
|
||||
name = "beatsaber-oneclick";
|
||||
runtimeInputs = [
|
||||
openssh
|
||||
libnotify
|
||||
curl
|
||||
jq
|
||||
];
|
||||
text = ''
|
||||
remote_script=${./remote.sh}
|
||||
''
|
||||
+ builtins.readFile ./beatsaber-oneclick.sh;
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs on the Steam Frame (piped over SSH): opens a OneClick link in the graphical
|
||||
# session, so the Frame's own handler (BSManager) downloads and installs it.
|
||||
#
|
||||
# Usage: remote.sh <url> [<beatsaver map id>...]
|
||||
#
|
||||
# With map ids, it then watches BSManager's log and map folders until those maps are
|
||||
# installed, and reports on stdout, one event per line:
|
||||
# received BSManager got the link
|
||||
# downloading <id> BSManager started downloading a map
|
||||
# done <id> the map's folder was (re)written and is complete
|
||||
# error <message> BSManager logged an error (may be unrelated)
|
||||
# result <id> new|already|missing
|
||||
set -euo pipefail
|
||||
|
||||
url=$1
|
||||
shift
|
||||
ids=("$@")
|
||||
uid=$(id -u)
|
||||
|
||||
# BSManager's default locations (installation folder not changed)
|
||||
bsm=$HOME/.local/share/BSManager
|
||||
logs=$HOME/.config/bs-manager/logs
|
||||
|
||||
# Give up when nothing has happened for this long (BSManager may need to start first)
|
||||
timeout=120
|
||||
# With maps that were already installed, finish once BSManager is quiet for this long
|
||||
quiet=10
|
||||
|
||||
for id in "${ids[@]}"; do
|
||||
if [[ ! $id =~ ^[0-9a-f]+$ ]]; then
|
||||
echo "Invalid map id: $id" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# An SSH session has no display. Borrow the display variables from a process in
|
||||
# the graphical session, preferring the Steam client.
|
||||
session_env() {
|
||||
local pid env
|
||||
for pid in $(pgrep -u "$uid" -x steam || true) $(pgrep -u "$uid"); do
|
||||
env=$(tr '\0' '\n' <"/proc/$pid/environ" 2>/dev/null) || continue
|
||||
if grep -qE '^(DISPLAY|WAYLAND_DISPLAY)=' <<<"$env"; then
|
||||
printf '%s\n' "$env"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
if ! env=$(session_env); then
|
||||
echo "No graphical session found on the Frame (is it signed in?)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
while IFS= read -r line; do
|
||||
export "${line?}"
|
||||
done < <(grep -E '^(DISPLAY|WAYLAND_DISPLAY|XAUTHORITY|XDG_CURRENT_DESKTOP|XDG_SESSION_TYPE|DBUS_SESSION_BUS_ADDRESS)=' <<<"$env")
|
||||
|
||||
opener=
|
||||
for candidate in gio xdg-open kde-open; do
|
||||
if command -v "$candidate" >/dev/null; then
|
||||
opener=$candidate
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ -z $opener ]]; then
|
||||
echo "No gio, xdg-open or kde-open on the Frame" >&2
|
||||
exit 1
|
||||
fi
|
||||
open_cmd=("$opener")
|
||||
[[ $opener == gio ]] && open_cmd+=(open)
|
||||
|
||||
map_dirs=("$bsm"/BSInstances/*/"Beat Saber_Data"/CustomLevels "$bsm"/SharedContent/SharedMaps/CustomLevels)
|
||||
|
||||
# Prints "<newest change time> <total size>" over a map's folders in all instances,
|
||||
# or "none". Change time (not mtime), as extracting may keep the zip's mtimes.
|
||||
map_state() {
|
||||
local d f t s newest=0 size=0 found=
|
||||
for d in "${map_dirs[@]}"; do
|
||||
for f in "$d/$1 ("*; do
|
||||
[[ -d $f ]] || continue
|
||||
found=1
|
||||
t=$(find "$f" -printf '%C@\n' | sort -n | tail -n1)
|
||||
t=${t%.*}
|
||||
((t > newest)) && newest=$t
|
||||
s=$(du -sb "$f" | cut -f1)
|
||||
size=$((size + s))
|
||||
done
|
||||
done
|
||||
if [[ -n $found ]]; then echo "$newest $size"; else echo none; fi
|
||||
}
|
||||
|
||||
# BSManager log lines written since we started. Continuation lines (stack traces)
|
||||
# don't start with a timestamp and are left out.
|
||||
log_since_start() {
|
||||
find "$logs" -name '*.log' -newermt "@$start" -print0 2>/dev/null |
|
||||
xargs -0r cat |
|
||||
awk -v s="[$start_log" 'substr($0, 1, 20) >= s'
|
||||
}
|
||||
|
||||
declare -A pre state prev
|
||||
for id in "${ids[@]}"; do
|
||||
pre[$id]=$(map_state "$id")
|
||||
state[$id]=pending
|
||||
prev[$id]=
|
||||
done
|
||||
|
||||
start=$(date +%s)
|
||||
start_log=$(date -d "@$start" '+%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# gio comes first: xdg-open (generic mode) mangles the quoted path in BSManager's Exec line.
|
||||
# Run it in its own scope, detached from this SSH session: if BSManager isn't running
|
||||
# yet, the handler starts it, and it must outlive the connection. Output goes to the
|
||||
# journal: journalctl --user -t beatsaber-oneclick
|
||||
systemd-run --user --scope --collect --quiet \
|
||||
setsid -f systemd-cat -t beatsaber-oneclick "${open_cmd[@]}" "$url" \
|
||||
</dev/null >/dev/null 2>&1
|
||||
|
||||
((${#ids[@]} > 0)) || exit 0
|
||||
|
||||
received=
|
||||
errors_seen=0
|
||||
last_activity=$start
|
||||
while :; do
|
||||
sleep 1
|
||||
now=$(date +%s)
|
||||
lines=$(log_since_start)
|
||||
|
||||
if [[ -z $received ]] && grep -q 'DEEP-LINK RECEIVED' <<<"$lines"; then
|
||||
received=1
|
||||
last_activity=$now
|
||||
echo received
|
||||
fi
|
||||
|
||||
mapfile -t errors < <(grep -F '[error]' <<<"$lines" || true)
|
||||
for err in "${errors[@]:errors_seen}"; do
|
||||
echo "error ${err#*\[error\] }"
|
||||
done
|
||||
errors_seen=${#errors[@]}
|
||||
|
||||
waiting=0
|
||||
all_done=1
|
||||
for id in "${ids[@]}"; do
|
||||
[[ ${state[$id]} == installed ]] && continue
|
||||
all_done=
|
||||
|
||||
if [[ ${state[$id]} == pending ]] && grep -qF " $id https://" <<<"$lines"; then
|
||||
state[$id]=downloading
|
||||
last_activity=$now
|
||||
echo "downloading $id"
|
||||
fi
|
||||
|
||||
# Done once the folder changed since we started and stayed the same for a second
|
||||
cur=$(map_state "$id")
|
||||
if [[ $cur != none ]] && ((${cur%% *} >= start)); then
|
||||
if [[ $cur == "${prev[$id]}" ]]; then
|
||||
state[$id]=installed
|
||||
last_activity=$now
|
||||
echo "done $id"
|
||||
continue
|
||||
fi
|
||||
prev[$id]=$cur
|
||||
last_activity=$now
|
||||
fi
|
||||
|
||||
# Maps that weren't installed yet, or are being downloaded, are worth waiting for
|
||||
if [[ ${state[$id]} == downloading || ${pre[$id]} == none ]]; then
|
||||
waiting=$((waiting + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
[[ -n $all_done ]] && break
|
||||
[[ -n $received ]] && ((waiting == 0 && now - last_activity >= quiet)) && break
|
||||
((now - last_activity >= timeout)) && break
|
||||
done
|
||||
|
||||
for id in "${ids[@]}"; do
|
||||
if [[ ${state[$id]} == installed ]]; then
|
||||
echo "result $id new"
|
||||
elif [[ ${pre[$id]} != none ]]; then
|
||||
echo "result $id already"
|
||||
else
|
||||
echo "result $id missing"
|
||||
fi
|
||||
done
|
||||
@@ -0,0 +1,78 @@
|
||||
{
|
||||
lib,
|
||||
fetchFromGitHub,
|
||||
rustPlatform,
|
||||
helix,
|
||||
helix-unwrapped,
|
||||
tree-sitter,
|
||||
pkgs,
|
||||
}:
|
||||
|
||||
# Helix from git, built with nixpkgs' packaging instead of the upstream flake. The
|
||||
# flake fetches every tree-sitter grammar without a hash at evaluation time. Here
|
||||
# they are pinned in grammars.json, so they are fetched once and kept in the store.
|
||||
#
|
||||
# To update: change rev and hash, set cargoHash to lib.fakeHash, build and copy the
|
||||
# expected hash, then run ./update-grammars.sh.
|
||||
let
|
||||
lockedGrammars = lib.importJSON ./grammars.json;
|
||||
|
||||
unwrapped = helix-unwrapped.overrideAttrs (
|
||||
final: prev: {
|
||||
version = "25.07.1-unstable-${final.src.rev}";
|
||||
|
||||
src = fetchFromGitHub {
|
||||
owner = "helix-editor";
|
||||
repo = "helix";
|
||||
rev = "f6f3eb1fe4a73933466f30175843059a4381a985";
|
||||
hash = "sha256-yfNBfV6HWAQU/xZXwrFoedsSYsDmCDIlTHRxAZI6teY=";
|
||||
};
|
||||
|
||||
cargoDeps = rustPlatform.fetchCargoVendor {
|
||||
inherit (final) src pname version;
|
||||
hash = "sha256-oo59HhwOS3EeV/YI+NWirEkdzD9pzMot2lgphZeOxfc=";
|
||||
};
|
||||
|
||||
# The mdbook 0.5 patch from nixpkgs targets the release
|
||||
patches = [ ];
|
||||
|
||||
# Report the commit in `hx --version`
|
||||
env = prev.env // {
|
||||
HELIX_NIX_BUILD_REV = final.src.rev;
|
||||
};
|
||||
|
||||
# The version check expects the release version string
|
||||
doInstallCheck = false;
|
||||
}
|
||||
);
|
||||
in
|
||||
(helix.override {
|
||||
helix-unwrapped = unwrapped;
|
||||
inherit lockedGrammars;
|
||||
grammarsOverlay = lib.composeManyExtensions [
|
||||
# Keep nixpkgs' fixes for its pinned grammars
|
||||
helix.defaultGrammarsOverlay
|
||||
|
||||
(final: prev: {
|
||||
# The tree-sitter.json fix is already included upstream
|
||||
tree-sitter-tlaplus = prev.tree-sitter-tlaplus.overrideAttrs { patches = [ ]; };
|
||||
})
|
||||
|
||||
# Grammars Helix uses that nixpkgs doesn't package yet
|
||||
(
|
||||
final: prev:
|
||||
lib.mapAttrs' (
|
||||
name: grammar:
|
||||
lib.nameValuePair "tree-sitter-${name}" (
|
||||
lib.makeOverridable tree-sitter.buildGrammar {
|
||||
language = name;
|
||||
version = lib.sources.shortRev grammar.nurl.args.rev;
|
||||
src = pkgs.${grammar.nurl.fetcher} grammar.nurl.args;
|
||||
location = grammar.subpath;
|
||||
}
|
||||
)
|
||||
) (lib.filterAttrs (name: _: !(prev ? "tree-sitter-${name}")) lockedGrammars)
|
||||
)
|
||||
];
|
||||
}).overrideAttrs
|
||||
{ passthru.unwrapped = unwrapped; }
|
||||
@@ -0,0 +1,79 @@
|
||||
"""
|
||||
Lock the tree-sitter grammars of a Helix languages.toml into grammars.json, in the
|
||||
format nixpkgs' helix package takes as `lockedGrammars`.
|
||||
|
||||
Based on nixpkgs' pkgs/by-name/he/helix/generate_grammars.py, but each fetch gets a
|
||||
timeout and falls back to fetching over git. Some forges (e.g. Codeberg) can hang
|
||||
while generating archives.
|
||||
|
||||
Usage: generate-grammars.py <languages.toml> <grammars.json>
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tomllib
|
||||
|
||||
TIMEOUT = 120
|
||||
|
||||
|
||||
async def nurl(url, rev, *extra):
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"nurl", *extra, url, rev, "--json",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
try:
|
||||
stdout, stderr = await asyncio.wait_for(proc.communicate(), TIMEOUT)
|
||||
except asyncio.TimeoutError:
|
||||
proc.kill()
|
||||
await proc.wait()
|
||||
raise RuntimeError(f"timed out after {TIMEOUT}s")
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError(stderr.decode().strip())
|
||||
return json.loads(stdout)
|
||||
|
||||
|
||||
async def lock(grammar, semaphore):
|
||||
source = grammar["source"]
|
||||
async with semaphore:
|
||||
try:
|
||||
result = await nurl(source["git"], source["rev"])
|
||||
except RuntimeError as e:
|
||||
print(f" {grammar['name']}: {e}, retrying over git", file=sys.stderr)
|
||||
result = await nurl(source["git"], source["rev"], "-f", "fetchgit")
|
||||
print(f" locked {grammar['name']}", file=sys.stderr)
|
||||
return grammar["name"].replace("_", "-"), {
|
||||
"nurl": result,
|
||||
"subpath": source.get("subpath"),
|
||||
}
|
||||
|
||||
|
||||
async def main(languages_toml, output):
|
||||
with open(languages_toml, "rb") as f:
|
||||
grammars = [
|
||||
g
|
||||
for g in tomllib.load(f).get("grammar", [])
|
||||
if "git" in g.get("source", {}) and "rev" in g["source"]
|
||||
]
|
||||
print(f"Locking {len(grammars)} grammars", file=sys.stderr)
|
||||
|
||||
semaphore = asyncio.Semaphore(os.cpu_count() or 4)
|
||||
results = await asyncio.gather(
|
||||
*(lock(g, semaphore) for g in grammars), return_exceptions=True
|
||||
)
|
||||
|
||||
failed = [(g["name"], r) for g, r in zip(grammars, results) if isinstance(r, Exception)]
|
||||
if failed:
|
||||
for name, error in failed:
|
||||
print(f"failed: {name}: {error}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
with open(output, "w") as f:
|
||||
json.dump(dict(sorted(results)), f, indent=2)
|
||||
f.write("\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main(*sys.argv[1:3]))
|
||||
File diff suppressed because it is too large.
Load diff
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regenerate grammars.json for the Helix revision pinned in default.nix
|
||||
set -euo pipefail
|
||||
|
||||
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
flake="$(git -C "$dir" rev-parse --show-toplevel)"
|
||||
|
||||
src="$(nix build --no-link --print-out-paths "$flake#helix-git.unwrapped.src")"
|
||||
|
||||
nix shell --inputs-from "$flake" nixpkgs#nurl nixpkgs#nix-prefetch-git nixpkgs#python3 -c \
|
||||
python3 -I "$dir/generate-grammars.py" "$src/languages.toml" "$dir/grammars.json"
|
||||
Reference in new issue
Block a user