Skip to content

-home: one directory, chosen deliberately, and an error when there is nowhere - #55

Merged
richlegrand merged 4 commits into
mainfrom
home-flag
Nov 10, 2026
Merged

richlegrand merged 4 commits into
mainfrom
home-flag

Conversation

@richlegrand

Copy link
Copy Markdown
Owner

Closes #41.

-home and BITBANG_HOME set the directory BitBang keeps its own files in:
the identity keypair, the link table, the device table connect and cp
read, and share state.

Three commits: the resolver, the flag, the startup line and README.

The order, first match wins

Set Directory used
-home <dir> exactly that
$BITBANG_HOME exactly that
$STATE_DIRECTORY exactly that -- systemd sets it from StateDirectory=
$XDG_STATE_HOME $XDG_STATE_HOME/bitbang
$HOME $HOME/.bitbang
none of them an error

The first three name the directory; the last two name a parent it hangs off.
internal/bbhome is the authority and says so in its package comment, so the
asymmetry does not get "fixed" later.

The bug

identityDir fell back to home = "." when os.UserHomeDir() failed, so a
service started without HOME wrote its private key relative to whatever
directory it started in -- / for an init script. That is how @lars18th's
OpenWRT listener ended up with its identity in /.bitbang, which took him
time to work out was a fallback rather than a choice.

It refuses now. identity.Dir returns an error and its five callers handle
it; link exits, since every one of its subcommands needs the path and
bbhome's message already says what to pass. Writing a keypair somewhere
nobody chose is worse than not writing it.

Notes on three decisions

The flag is on every command, not just serve. The directory is not
serve's, so a flag on one command leaves the rest needing the env var anyway,
and forgetting it once on bitbang link would quietly edit a different table.
Registered with flag.Func so it applies during Parse, before any command
body can ask bbhome where to look.

The share worker gets it in its argv. It runs in a tmux pane, so its
environment is the tmux server's: BITBANG_HOME would not reach it and even
HOME can differ, which means the worker could already have loaded a
different identity than the process that spawned it.

The startup line is printed once, not from ready(). ready() reprints
on every reconnect, and the directory cannot move under a running listener, so
this is one line per process rather than one per reconnect -- it does not undo
#34. It always names the directory, default included, and names the source
only when that is not the plain home directory.

Home: /home/you/.bitbang
Home: /var/lib/bitbang (STATE_DIRECTORY)

Verified against the built binary

Not only unit tests: -home and BITBANG_HOME each land in the named
directory, STATE_DIRECTORY is used as given, the flag beats the environment,
-home directory appears in help on every command, and with none of them and
no HOME it exits 1 saying what to pass instead of writing to the working
directory.

Twelve table cases in internal/bbhome cover each rung and its source, the
refusal, and that Root re-reads the environment rather than caching -- the
first draft cached it, which let one test read the developer's real device
table instead of its own temp directory.

Not in this

The URL file from #54. That needs a directory that does not move, which is
what this provides, but it is its own change.

The directory holding the keypair, the link table, the device table and
share state was computed in three places, each spelling `~/.bitbang`
itself. One of them fell back to "." when there was no home directory, so
a service started without HOME wrote its private key relative to whatever
directory it started in -- `/` for an init script, which is how an OpenWRT
listener ended up with its identity in /.bitbang.

internal/bbhome resolves it once, in an order a README can state: the
-home flag (next commit), BITBANG_HOME, systemd's STATE_DIRECTORY,
XDG_STATE_HOME/bitbang, ~/.bitbang, and then an error. The first three
name the directory; the last two name a parent it hangs off.

The error is the point. Writing a private key somewhere nobody chose is
worse than refusing to write it, so identity.Dir returns an error now and
its five callers handle it. `link` exits, since every one of its
subcommands needs the path and bbhome's message already says what to do.

Resolved on every call rather than cached. The first draft cached it,
which survives a test pointing HOME at a temp directory: the first test to
resolve decided for all the others, and one of them went looking in the
real device table.

Groundwork for #41.
Not just `serve`. The directory holds the device table `connect` and `cp`
read and the link table `link` edits, so a flag on one command would leave
the others needing the environment variable anyway, and forgetting it once
on `bitbang link` would quietly edit a different table.

Registered with flag.Func so it applies during Parse, before any command
body can ask where to look. Nothing to thread through, no order to get
wrong.

The share worker takes it too, and gets it passed explicitly. It runs in a
tmux pane, so its environment is the tmux server's rather than ours:
BITBANG_HOME would not reach it, and even HOME can differ, which means the
worker could already have loaded a different identity than the process
that spawned it.

Verified against the built binary: -home and BITBANG_HOME both land in the
named directory, STATE_DIRECTORY is used as given, the flag beats the
environment, and with none of them and no HOME it exits 1 saying what to
pass rather than writing to the working directory.

For #41.
Six ways to choose the directory means an operator whose files are not
where they expected has six things to check. The listener names it:

    Home: /home/you/.bitbang
    Home: /var/lib/bitbang (STATE_DIRECTORY)

Always, including the default. Somebody running this for the first time has
the same question as somebody who configured it, and #41 began with an
operator not knowing where his files had gone. The source is named only
when it is not the plain home directory, because "(HOME)" after a path
under ~ says nothing the path did not.

This does not undo #34: it is printed once at startup and deliberately not
from ready(), which reprints on every reconnect. One line per process, not
one per reconnect.

The README section is the order itself, as a table, plus the systemd unit
that needs no BitBang-specific configuration because StateDirectory= is
already one of the rungs.

Closes the written part of #41.
Two Windows assumptions in the new tests, neither in the code they test.

filepath.Clean turns a forward slash into a backslash there, so a `want`
written as "/flag" could never match. The composed expectations were
already fine, since filepath.Join does the same thing on both sides; it is
the literals that needed FromSlash.

And os.UserHomeDir reads USERPROFILE on Windows, not HOME, so every case
expecting the home rung errored instead -- the ladder fell through to "no
home directory". Set both, which is what devices_test.go already does.

Found by CI, which is the only thing that can run these: on linux
FromSlash is the identity function and USERPROFILE is never read, so
neither mistake is reproducible locally.
@richlegrand
richlegrand merged commit 080dc83 into main Nov 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question: How to use it as a daemon?

1 participant