;; Custom nix-daemon service that raises its NOFILE rlimit.
;;
;; The stock (gnu services nix) nix-daemon is started by shepherd with the
;; default 1024/4096 open-files rlimit. Build processes run as @nixbld are
;; spawned as children of the daemon, so they inherit that limit — PAM limits
;; for @nixbld are ineffective because nixbld users never log in.
;;
;; This module re-exports a service type that inherits from nix-service-type
;; but replaces its shepherd-root-service-type extension with one that passes
;; #:resource-limits to make-forkexec-constructor.
(define-module (ruther services nix)
#:use-module (srfi srfi-1)
#:use-module (guix gexp)
#:use-module (gnu services)
#:use-module (gnu services nix)
#:use-module (gnu services shepherd)
#:export (nix-service-type/raised-nofile))
;; The nix-configuration-* accessors are not exported from (gnu services nix),
;; so reach in with @@.
(define nix-configuration-package
(@@ (gnu services nix) nix-configuration-package))
(define nix-configuration-build-directory
(@@ (gnu services nix) nix-configuration-build-directory))
(define nix-configuration-extra-options
(@@ (gnu services nix) nix-configuration-extra-options))
(define (nix-shepherd-service/raised-nofile config)
(let ((package (nix-configuration-package config))
(build-directory (nix-configuration-build-directory config))
(extra-options (nix-configuration-extra-options config)))
(list
(shepherd-service
(provision '(nix-daemon))
(documentation "Run nix-daemon with a raised NOFILE rlimit.")
(requirement '(user-processes file-system-/nix/store))
(start #~(make-forkexec-constructor
(list (string-append #$package "/bin/nix-daemon")
#$@extra-options)
#:environment-variables
(list (string-append "TMPDIR=" #$build-directory)
"PATH=/run/current-system/profile/bin")
#:resource-limits
'((nofile 1048576 1048576))))
(respawn? #f)
(stop #~(make-kill-destructor))))))
(define nix-service-type/raised-nofile
(service-type
(inherit nix-service-type)
(extensions
(cons (service-extension shepherd-root-service-type
nix-shepherd-service/raised-nofile)
(filter (lambda (ext)
(not (eq? (service-extension-target ext)
shepherd-root-service-type)))
(service-type-extensions nix-service-type))))))