summary refs log tree commit diff
path: root/pkgs/tools/system/zram-generator/default.nix
blob: 3d55d7c19613ecd5efca996bd89a9e8ca4c78d43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{ lib
, stdenv
, fetchFromGitHub
, rustPlatform
, pkg-config
, ronn
, systemd
, kmod
, nixosTests
}:

rustPlatform.buildRustPackage rec {
  pname = "zram-generator";
  version = "1.1.2";

  src = fetchFromGitHub {
    owner = "systemd";
    repo = pname;
    rev = "v${version}";
    hash = "sha256-n+ZOWU+sPq9DcHgzQWTxxfMmiz239qdetXypqdy33cM=";
  };

  # RFE: Include Cargo.lock in sources
  # https://github.com/systemd/zram-generator/issues/65
  cargoLock.lockFile = ./Cargo.lock;

  postPatch = ''
    cp ${./Cargo.lock} Cargo.lock
    substituteInPlace Makefile \
      --replace 'target/$(BUILDTYPE)' 'target/${stdenv.hostPlatform.rust.rustcTargetSpec}/$(BUILDTYPE)'
    substituteInPlace src/generator.rs \
      --replace 'Command::new("systemd-detect-virt")' 'Command::new("${systemd}/bin/systemd-detect-virt")' \
      --replace 'Command::new("modprobe")' 'Command::new("${kmod}/bin/modprobe")'
  '';

  nativeBuildInputs = [
    pkg-config
    ronn
  ];

  buildInputs = [
    systemd
  ];

  preBuild = ''
    # embedded into the binary at build time
    # https://github.com/systemd/zram-generator/blob/v1.1.2/Makefile#LL11-L11C56
    export SYSTEMD_UTIL_DIR=$($PKG_CONFIG --variable=systemdutildir systemd)
  '';

  dontCargoInstall = true;

  installFlags = [
    "-o program" # already built by cargoBuildHook
    "PREFIX=$(out)"
    "SYSTEMD_SYSTEM_UNIT_DIR=$(out)/lib/systemd/system"
    "SYSTEMD_SYSTEM_GENERATOR_DIR=$(out)/lib/systemd/system-generators"
  ];

  passthru = {
    tests = {
      inherit (nixosTests) zram-generator;
    };
    updateScript = ./update.sh;
  };

  meta = with lib; {
    homepage = "https://github.com/systemd/zram-generator";
    license = licenses.mit;
    description = "Systemd unit generator for zram devices";
    maintainers = with maintainers; [ nickcao ];
  };
}