Search code examples
weathernixosi3

How to add weather to i3status in NixOS


Adding weather to the status bar in i3 can be done in several ways, including:

  • py3status
  • piping i3status to a custom bash script

i3status does not allow including arbitrary shell commands in the configuration file. NixOS environment for Python requires further configuration, and when I pipe i3status I lose the color formatting. How do I preserve color formatting and add weather without adding additional i3 extensions?


Solution

  • Add a shell script to /etc/nixos/i3/weather.sh (modified from Reddit user @olemartinorg):

    #!/bin/sh
    # weather.sh
    # shell script to prepend i3status with weather
    
    i3status -c /etc/nixos/i3/i3status.conf | while :
    do
      read line
      weather=$(cat ~/.weather.cache)
      weather_json='"name":"weather","color":"#FFFFFF", "full_text":'
      weather_json+=$(echo -n "$weather" | python -c 'import json,sys; print json.dumps(sys.stdin.read())')
      weather_json+='},{'
    
      # Inject our JSON into $line after the first [{
      line=${line/[{/[{$weather_json}
      echo "$line" || exit 1
    done
    

    Create a cronjob in your NixOs configuration.nix:

    services.cron = {
        enable = true;
        systemCronJobs = [
          "*/5 * * * *      USERNAME    . /etc/profile; curl -s wttr.in/Osnabrueck?format=3 > ~/.weather.cache"
        ];
      };
    

    Replace "Osnabrueck" with your city name, and USERNAME with your username. This creates a file .weather.cache which will contain the local weather as a one-liner.

    Finally, update i3.conf, replacing i3status with the path to your script:

    bar {
        status_command /etc/nixos/i3/weather.sh
        tray_output primary
    }
    

    nixos-rebuild switch and start i3 ($mod+Shift+R). You should now see your weather at the bottom (or wherever your i3 status bar displays).