Skip to content

Generate systemd units from NixOS-style descriptions

License

Notifications You must be signed in to change notification settings

holocene-systems/systemd-nix

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

systemd-nix

Generate systemd units from NixOS-style descriptions

Descriptions of functions

Given a NixOS-style description of a systemd unit, like

{
  description = "A daemon to upload nix store paths to a remote store asynchronously";
  wantedBy = [ "default.target" ];
  path = with pkgs; [ nix upload-daemon ];
  script =
    ''upload-daemon \
    --target "ssh://some-remote-server" \
    --unix "/tmp/upload-daemon.sock" \
    -j $(nproc) \
    +RTS -N$(nproc)'';
  serviceConfig.Restart = "always";
}

mkService

Will generate a systemd service description, like

[Unit]
Description=A daemon to upload nix store paths to a remote store asynchronously

[Service]
Environment="LOCALE_ARCHIVE=/nix/store/<...>/lib/locale/locale-archive"
Environment="PATH=<...>"
Environment="TZDIR=<...>/share/zoneinfo"

ExecStart=/nix/store/<...>-unit-script-upload-daemon-start/bin/upload-daemon-start
Restart=always

mkUserService

Will generate an activation script that [re]installs and [re]starts the service generate as described above.

Usage example

{
  inputs = {
    systemd-nix = {
      url = github:serokell/systemd-nix;
      inputs.nixpkgs.follows =
        "nixpkgs"; # Make sure the nixpkgs version matches
    };
    deploy.url = github:serokell/deploy;
  };

  outputs = { self, nixpkgs, systemd-nix, deploy }:
    {
      # `nix run` will deploy
      inherit (deploy) defaultApp;
      deploy.nodes.example = {
        hostname = "localhost";
        profiles.hello = {
          path = systemd-nix.lib.x86_64-linux.mkUserService "hello" {
            description = "Produce a greeting and exit";
            path = [ nixpkgs.legacyPackages.x86_64-linux.hello ];
            serviceConfig = {
              Type = "oneshot";
              ExecStart = "hello";
            };
          };
          # Just to test that it's working
          activate = "$PROFILE/bin/activate";
        };
      };
    };
}

License

systemd-nix is licensed under the Mozilla Public License Version 2.0. You can read it in ./LICENSE.

About Serokell

systemd-nix is maintained and funded with ❤️ by Serokell. The names and logo for Serokell are trademark of Serokell OÜ.

We love open source software! See our other projects or hire us to design, develop and grow your idea!

About

Generate systemd units from NixOS-style descriptions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Nix 100.0%