NixOs: nixos가 아닌 OS에서 서비스를 사용하고 결국에는 사용자 권한만 사용합니다.

NixOs: nixos가 아닌 OS에서 서비스를 사용하고 결국에는 사용자 권한만 사용합니다.

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에서 어떻게 컴파일하고 실행할 수 있습니까?

답변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_PATHNIX_PATH=nixpkgs=/your/nixpkgs/copy/:othervar=thepath여기. 이 파일에는 환경 변수 에 nixos-config 항목을 추가하는 데 <nixos-config>사용되는 가 필요합니다 . 그런 다음 가 없으면 전체 nixos를 빌드하려고 시도하므로 이 서비스 단위만 원한다고 지정합니다.-INIX_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.servicesDsystemd.services.users

관련 정보