Stable macOS hostname conditionals, interoperable with Linux #4879
Replies: 1 comment 3 replies
-
|
I found stat to be the safest and fastest method - this way a shell isn't spun if you don't need it. Pros: fast, built-in, will work on any environment (even on windows) Example on MacOS: $ chezmoi execute-template '{{ lookPath "/usr/sbin/scutil" }}' && echo
/usr/sbin/scutilfinal template will look something like: However, I'm not sure I would do this in a template at all. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm managing some config differences in the same files between a specific macOS system and Linux hosts.
I hit the snag where the macOS hostname is not stable or reliable for configuration usage, which is documented in the macOS-specific user guide.
It took me some time however to figure out how to actually use the recommended
scutilmethod in files that are shared across machines, since my Linux hosts would throw errors when applying config files because they don't have thescutilprogram.What I landed on after some iterations is a callable shared template:
.chezmoitemplates/computerName:{{ output "/bin/sh" "-c" (list "command" "-v" "scutil" "&&" "(scutil" "--get" "ComputerName)" "||" "echo" "''" | join " ") | trim }}This uses the
outputfunction with a more complex shell expression that first checks for the presence of thescutilcommand. Another discussion showed me how to do this withlistandjoin.Then, in any template where I need to add a conditional:
This seems to work well, maybe it can help others!
Beta Was this translation helpful? Give feedback.
All reactions