非 Nix OS 上のモジュールのように記述されたサービスを実行する方法を知りたいです。たとえば、次のようなファイルがあるとします。
{config, pkgs, ... }:
{
systemd.user.services.mytestservice = {
description = "Mytestservice";
script = "while true; do echo 'YES'; sleep 1; done";
wantedBy = [ "default.target" ];
};
}
(または最終的にはユーザーなしでsystemd.services.mytestservice =
:)
どうすればこれをコンパイルして、非 nixos OS 上で、最終的には非 root ユーザーによって実行できるでしょうか?
答え1
clever
謝辞:全ての説明に感謝します!
まず、次の場所に設定ファイルを記述しますmyconfiguration.nix
。
{config, pkgs, ... }:
{
# You can actually remove the user, and still use it
# as a user if you link it in ~/.config/systemd/user/
# (do not forget to remove the `user` it in anything.nix
# as well)
systemd.user.services.mytestservice = {
description = "Mytestservice";
script = "while true; do echo 'YES'; sleep 1; done";
# Or:
# serviceConfig = {
# ExecStart = "${pkgs.bash}/bin/bash -c \"while true; do echo 'YES'; sleep 1; done\"";
# };
wantedBy = [ "default.target" ];
};
}
次に、いくつかのことを行うことができます。
- コンパイルする
- インストールする
コンパイルのみを実行するには、次のようにします。
nix-build '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -A 'config.systemd.user.units."mytestservice.service".unit'
これは、オンラインで入手可能なdefault.nix
フォルダ内のファイルをロードします/your/nixpkgs/copy/nixos/
(のパスを取得するには、などのいくつかの「サブキー」を含むnixpkgs
変数をチェックします)。NIX_PATH
NIX_PATH=nixpkgs=/your/nixpkgs/copy/:othervar=thepath
こここのファイルには、環境変数に nixos-config エントリを追加するために<nixos-config>
使用するも必要です。 がない場合、完全な nixos を構築しようとするため、このサービス ユニットのみが必要であることを指定します。-I
NIX_PATH
-A
mytestservice.service
次のようなファイルが生成されます。
$ cat result/mytestservice.service
[Unit]
Description=Mytestservice
[Service]
Environment="LOCALE_ARCHIVE=/nix/store/zzhablipzgpv8mvlcvagqjnham6lr944-glibc-locales-2.27/lib/locale/locale-archive"
Environment="PATH=/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/bin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/bin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/bin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/bin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/bin:/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/sbin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/sbin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/sbin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/sbin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/sbin"
Environment="TZDIR=/nix/store/20wmykp8fj2izxdj8lic8ggcfpdid5ka-tzdata-2019a/share/zoneinfo"
ExecStart=/nix/store/1f0wk7l4p7xv257dci8xxqz1k8nai9va-unit-script-mytestservice-start
これを呼び出したい場合は、インストールする必要があります。
nix-env -f '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -iA 'config.systemd.user.units."mytestservice.service".unit'
mytestservice.service
これにより、が にリンクされます~/.nix-profile/mytestservice.service
。ただし、systemctl は が にあることを期待している~/.config/systemd/user/
ため、次のようにリンクします。
ln -s ~/.nix-profile/mytestservice.service ~/.config/systemd/user/
次にデーモンをリロードして、試してみましょう。
systemctl --user daemon-reload
systemctl --user start mytestservice.service
ただし、ビルド/インストール コマンドは複雑で入力に時間がかかるため、anything.nix
すべてをビルドするファイル ( など) を作成することもできます。
let
eval = import <nixpkgs/nixos> {
configuration = ./myconfiguration.nix;
};
pkgs = import <nixpkgs>{};
in pkgs.buildEnv {
name = "things";
paths = [
eval.config.systemd.user.units."mytestservice.service".unit
];
}
これで、次のようにコンパイルできます:
nix-build anything.nix
そしてインストールする
nix-env -f anything.nix -i things
次のような方法で、他の方法でインストールしたファイルを最初に削除する必要がある場合があることに注意してください。
nix-env --query
nix-env --uninstall unit-mytestservice.service
最後に、両方のコードsystemd.services
ともsystemd.services.users
この方法で使用できるようです :D