# Alternative to Salt grains or Puppet facts?

**URL:** https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308
**Category:** Uncategorized
**Created:** [June 28, 2019, 9:36am UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308 "2019-06-28T09:36:46Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![dR3b](https://discourse.nixos.org/user_avatar/discourse.nixos.org/dr3b/32/32123_2.png) [@dR3b](https://discourse.nixos.org/u/dR3b)
#### Post date: [June 28, 2019, 9:36am UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/1 "2019-06-28T09:36:46Z")

</div>

Hi,

Is there an alternative for such a system at NixOS?  
I would like to create a dynamic `/etc/motd`.

As an example:

```auto
Welcome to {{ grains['fqdn'] }}

  - THIS SERVER IS MANAGED BY SALTSTACK!
  - DO NOT CHANGE ANYTHING LOCALLY!

IP: {% for ip in grains['ipv4'] %}{% if ip != '127.0.0.1' %}{{ ip }} {% endif %}{% endfor %}
CPU: {{ grains['num_cpus'] }} x {{ grains['cpu_model'] }}
MEM: {{ grains['mem_total'] }} MB

OS: {{ grains['lsb_distrib_description'] }}
Kernel: {{ grains['kernelversion'] }}

Virtual: {{ grains['virtual'] }}
Salt: {{ grains['saltversion'] }}
Master: {{ grains['master'] }}

```

---

<div class="post-metadata">

### Author: ![Mic92](https://discourse.nixos.org/user_avatar/discourse.nixos.org/mic92/32/5265_2.png) [@Mic92](https://discourse.nixos.org/u/Mic92)
#### Post date: [June 28, 2019, 10:10am UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/2 "2019-06-28T10:10:05Z")

</div>

You won’t get all information from the nixos configuration you use in your motd is using runtime values.  
However you can explore some information using nix repl:

```plaintext
$ nix repl '<nixpkgs/nixos>'
Welcome to Nix version 2.2.2. Type :? for help.

Loading '<nixpkgs/nixos>'...
Added 6 variables.
nix-repl> config.networking.hostName
"turingmachine"

```

For the remaining information you could also use an executable motd (I think there was a way to have a script instead of a static text file).

---

<div class="post-metadata">

### Author: ![manveru](https://discourse.nixos.org/user_avatar/discourse.nixos.org/manveru/32/23_2.png) [@manveru](https://discourse.nixos.org/u/manveru)
#### Post date: [June 28, 2019, 12:39pm UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/3 "2019-06-28T12:39:17Z")

</div>

The MOTD has to be a static text file, so you can’t just put a script in there. There’s also no place in the NixOS configuration to put the CPU/Memory (unless you count `nix.maxJobs`, which doesn’t neccesarily reflect your number of cores and certainly doesn’t tell you the speed).

The usual approach is to generate the file regularly with a cronjob or systemd timer. The benefit of a timer is that you could potentially trigger it when your network changes or you switch to a new configuration.

The other option is to basically output it `programs.bash.loginShellInit`, but of course that won’t work exactly the same for other shells.

For the static version you could use this:

```auto
  users.motd = with config; ''
    Welcome to ${networking.hostName}

    - This server is managed by NixOS
    - All changes are futile

    OS: NixOS ${system.nixos.release} (${system.nixos.codeName})
    Version: ${system.nixos.version}
    Kernel: ${boot.kernelPackages.kernel.version}
  '';

```

The output looks like this:

```auto
Welcome to kappa

- This server is managed by NixOS
- All changes are futile

OS: NixOS 19.03 (Koi)
Version: 19.03.172921.d77e3bd6613
Kernel: 4.19.52

```

---

<div class="post-metadata">

### Author: ![markuskowa](https://discourse.nixos.org/user_avatar/discourse.nixos.org/markuskowa/32/719_2.png) [@markuskowa](https://discourse.nixos.org/u/markuskowa)
#### Post date: [June 28, 2019, 12:50pm UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/4 "2019-06-28T12:50:53Z")

</div>

I use custom banner scripts to display dynamic data on SSH login. The script is then included in `loginShellInit` (as already indicated by manveru). The caveat here is to make sure that the banner is only shown for interactive user logins or otherwise it would break tools like nixops, scp, etc.

```auto
environment.loginShellInit = ''                                                                                                    
   # disable for user root and non-interactive tools                                                                                
   if [`id -u` != 0]; then                                                                                                        
     if ["x''${SSH_TTY}" != "x"]; then                                                                                            
       ${banner}/bin/banner                                                                                                         
     fi                                                                                                                             
   fi                                                                                                                               
 '';

```

---

<div class="post-metadata">

### Author: ![dR3b](https://discourse.nixos.org/user_avatar/discourse.nixos.org/dr3b/32/32123_2.png) [@dR3b](https://discourse.nixos.org/u/dR3b)
#### Post date: [June 28, 2019, 3:01pm UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/5 "2019-06-28T15:01:53Z")

</div>

Many thanks for all inputs.

@manveru  
This looks very good for me but I get the following error when I start `nixos-rebuild dry-run`:

```auto
building the system configuration...
error: undefined variable 'nixos' at /etc/nixos/modules/defaults/users.nix:35:22
(use '--show-trace' to show detailed location information)

```

I’ve only been using NixOS for a day 🙂

---

<div class="post-metadata">

### Author: ![manveru](https://discourse.nixos.org/user_avatar/discourse.nixos.org/manveru/32/23_2.png) [@manveru](https://discourse.nixos.org/u/manveru)
#### Post date: [June 28, 2019, 3:24pm UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/6 "2019-06-28T15:24:01Z")

</div>

Sorry, somehow my clipboard contents were switched, I updated the post with the working version.

---

<div class="post-metadata">

### Author: ![tokudan](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/t/dc4da7/32.png) [@tokudan](https://discourse.nixos.org/u/tokudan)
#### Post date: [June 28, 2019, 4:08pm UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/7 "2019-06-28T16:08:44Z")

</div>

@markuskowa  
This is probably an easier solution to avoid producing output that programs cannot deal with. It checks if stdout (file descriptor 1) is a terminal. If a program explicitely requests a terminal, then it should be able to deal with output intended for humans.

```auto
if [! -t 1]; then
${banner}/bin/banner
fi

```

This is the result:

```auto
$ ssh freya test -t 1 && echo terminal

$ ssh freya
[root@freya:~]# test -t 1 && echo terminal
terminal

```

---

<div class="post-metadata">

### Author: ![markuskowa](https://discourse.nixos.org/user_avatar/discourse.nixos.org/markuskowa/32/719_2.png) [@markuskowa](https://discourse.nixos.org/u/markuskowa)
#### Post date: [June 29, 2019, 11:04am UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/8 "2019-06-29T11:04:00Z")

</div>

@tokudan Thanks! I didn’t know it can be made that simple.

---

<div class="post-metadata">

### Author: ![dR3b](https://discourse.nixos.org/user_avatar/discourse.nixos.org/dr3b/32/32123_2.png) [@dR3b](https://discourse.nixos.org/u/dR3b)
#### Post date: [July 2, 2019, 6:32am UTC](https://discourse.nixos.org/t/alternative-to-salt-grains-or-puppet-facts/3308/9 "2019-07-02T06:32:47Z")

</div>

@manveru  
Thanks!

@all  
Is it possible to display the status of a systemd unit?
