Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

bud is unable to detect hostname on macOS #23

Open
montchr opened this issue Sep 21, 2021 · 5 comments
Open

bud is unable to detect hostname on macOS #23

montchr opened this issue Sep 21, 2021 · 5 comments

Comments

@montchr
Copy link

montchr commented Sep 21, 2021

Moved from divnix/digga#375 because this happens with every bud command I've tried.

Expected Behavior

On macOS/darwin systems, bud should detect my system's hostname.

Current Behavior

Running bud commands on macOS 11.6 results in an error caused by calling hostname with invalid options.

bud up --help

The output of bud up --help also indicates bud was not able to get a hostname, as the name is not included in the path mentioned in the usage section:

❯ bud up --help
hostname: illegal option -- -
usage: hostname [-fs] [name-of-host]
/nix/store/hzqrpf31gf060ka40hwpqpah79n54hgm-devshell-dir/bin/bud: line 8:  -1: substring expression < 0

Usage: up   Generate /path/to/repo/hosts//default.nix

Description:

bud --help

❯ bud --help
hostname: illegal option -- -
usage: hostname [-fs] [name-of-host]
/nix/store/hzqrpf31gf060ka40hwpqpah79n54hgm-devshell-dir/bin/bud: line 8:  -1: substring expression < 0

...

bud repl

❯ bud repl
hostname: illegal option -- -
usage: hostname [-fs] [name-of-host]
/nix/store/hzqrpf31gf060ka40hwpqpah79n54hgm-devshell-dir/bin/bud: line 8:  -1: substring expression < 0
Welcome to Nix version 2.4pre20210707_02dd6bb. Type :? for help.

Loading '/nix/store/q35fmyf6l8d5gc7khgmpqzj84q6l6wlj-repl.nix'...
Added 4 variables.

bud get (via devos)

DevOS' quick start guide suggests running the following command:

nix-shell https://github.com/divnix/devos/archive/master.tar.gz -A shell \
  --run "bud get master"

This also results in a similar error:

...
created 129 symlinks in user environment
building '/nix/store/izybzyn94b2plalg380jzqq4nnqllsyl-nixos-entrypoint.drv'...
fatal: not a git repository (or any of the parent directories): .git
hostname: illegal option -- -
usage: hostname [-fs] [name-of-host]
/nix/store/83cv4fcjj56sch8z0njlrrf8wiv8kim2-devshell-dir/bin/bud: line 8:  -1: substring expression < 0
11.09s user 30.55s system 5% cpu 13:06.38s total

Steps to Reproduce

On macOS, run any of the commands mentioned above.

Your Environment

  • macOS 11.6 on MacBookPro16,2 (Intel)

I'm guessing Darwin's hostname utility behaves differently than it would on NixOS or another Linux system.

This might also apply to other systems using the BSD utilities?

❯ which hostname
/bin/hostname
❯ hostname --help
hostname: illegal option -- -
usage: hostname [-fs] [name-of-host]
@montchr montchr changed the title bud calls darwin hostname utility with invalid options bud is unable to detect hostname on macOS Sep 21, 2021
@nrdxp
Copy link

nrdxp commented Sep 21, 2021

are you trying to deploy NixOS systems from darwin, or what is the usecase here? Unfortunately we still don't have great nix-darwin support in general since we don't have any MacOS users on the core team.

@blaggacao
Copy link
Collaborator

blaggacao commented Sep 22, 2021

The particular implementation is towards the end of:

bud/module.nix

Lines 75 to 88 in 26ba276

host =
let
partitionString = sep: s:
builtins.filter (v: builtins.isString v) (builtins.split "${sep}" s);
reversePartition = s: lib.reverseList (partitionString "\\." s);
rebake = l: builtins.concatStringsSep "." l;
in
if (hostConfig != null && hostConfig.networking.domain != null) then
rebake (reversePartition hostConfig.networking.domain + [ hostConfig.networking.hostName ])
else if hostConfig != null then
hostConfig.networking.hostName
# fall back to reverse dns from hostname --fqdn command
else "$(IFS='.'; parts=($(hostname --fqdn)); IFS=' '; HOST=$(for (( idx=\${#parts[@]}-1 ; idx>=0 ; idx-- )) ; do printf \"\${parts[idx]}.\"; done); echo \${HOST:: -1})"
;

We internally need to work with reverse DNS of the fqdn to "reliably" match hosts's to the host raking contract: nested folders are raked as reverse DNS, like so:

com
  example
    host1
    host2
net
  example
    host1
    host2

(sorry for that bash sequence)

@montchr
Copy link
Author

montchr commented Sep 22, 2021

are you trying to deploy NixOS systems from darwin, or what is the usecase here?

@nrdxp use case is working with a flake providing an output matching my current darwin host's hostname. The flake provides a set of configurations for nix-darwin and home-manager. Essentially just dotfiles. But I'd like to reuse these profiles on NixOS systems as I build them out (via VMs or Linodes). So at least on darwin, I'd only be using the commands that aren't NixOS-specific (e.g. bud repl or bud switch if it could be used with darwin-rebuild)

@blaggacao Thanks, that points me in the right direction. I'm going to assume I'm doing something kind of wrong in the process of setting up this darwin host, as I don't think I should have ended up hitting that call to hostname at all.

@blaggacao
Copy link
Collaborator

blaggacao commented Oct 10, 2021

@montchr It should alwasy hit hostname unless you'd be running already from within the host that you (still) want to build.

That is: in the devshell, it should always try to figure out your current host via hostname.

Could you think of a BSD compatible implementation?

@KarolisL
Copy link

I think that hostname -f works on both BSD (at least on Darwin) and Linux. Wouldn't that work?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants