首页 > 解决方案 > 如何在 NixOS 中将天气添加到 i3status

问题描述

在 i3 中向状态栏添加天气可以通过多种方式完成,包括:

i3status 不允许在配置文件中包含任意 shell 命令。用于 Python 的 NixOS 环境需要进一步配置,当我使用管道时,i3status我会丢失颜色格式。如何在不添加额外 i3 扩展的情况下保留颜色格式并添加天气?

标签: weathernixosi3

解决方案


添加一个 shell 脚本/etc/nixos/i3/weather.sh(从 Reddit 用户@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

在你的 NixOs 中创建一个 cronjob configuration.nix

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

将“Osnabrueck”替换为您的城市名称和USERNAME您的用户名。这将创建一个文件,该文件.weather.cache将包含当地天气作为单行。

最后,更新i3.conf,替换i3status为脚本的路径:

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

nixos-rebuild switch并启动 i3 ( $mod+Shift+R)。您现在应该在底部(或 i3 状态栏显示的任何位置)看到您的天气。


推荐阅读