1 {config, pkgs, lib, ...}:
3 cfg = config.services.spark;
9 enable = lib.mkEnableOption "Spark master service";
12 description = "Address the spark master binds to.";
13 default = "127.0.0.1";
16 restartIfChanged = lib.mkOption {
17 type = lib.types.bool;
19 Automatically restart master service on config change.
20 This can be set to false to defer restarts on clusters running critical applications.
21 Please consider the security implications of inadvertently running an older version,
22 and the possibility of unexpected behavior caused by inconsistent versions across a cluster when disabling this option.
26 extraEnvironment = lib.mkOption {
27 type = lib.types.attrsOf lib.types.str;
28 description = "Extra environment variables to pass to spark master. See spark-standalone documentation.";
31 SPARK_MASTER_WEBUI_PORT = 8181;
32 SPARK_MASTER_OPTS = "-Dspark.deploy.defaultCores=5";
37 enable = lib.mkEnableOption "Spark worker service";
38 workDir = lib.mkOption {
39 type = lib.types.path;
40 description = "Spark worker work dir.";
41 default = "/var/lib/spark";
43 master = lib.mkOption {
45 description = "Address of the spark master.";
46 default = "127.0.0.1:7077";
48 restartIfChanged = lib.mkOption {
49 type = lib.types.bool;
51 Automatically restart worker service on config change.
52 This can be set to false to defer restarts on clusters running critical applications.
53 Please consider the security implications of inadvertently running an older version,
54 and the possibility of unexpected behavior caused by inconsistent versions across a cluster when disabling this option.
58 extraEnvironment = lib.mkOption {
59 type = lib.types.attrsOf lib.types.str;
60 description = "Extra environment variables to pass to spark worker.";
63 SPARK_WORKER_CORES = 5;
64 SPARK_WORKER_MEMORY = "2g";
68 confDir = lib.mkOption {
69 type = lib.types.path;
70 description = "Spark configuration directory. Spark will use the configuration files (spark-defaults.conf, spark-env.sh, log4j.properties, etc) from this directory.";
71 default = "${cfg.package}/conf";
72 defaultText = lib.literalExpression ''"''${package}/conf"'';
74 logDir = lib.mkOption {
75 type = lib.types.path;
76 description = "Spark log directory.";
77 default = "/var/log/spark";
79 package = lib.mkPackageOption pkgs "spark" {
81 spark.overrideAttrs (super: rec {
86 url = "mirror://apache/spark/"''${pname}-''${version}/''${pname}-''${version}-bin-without-hadoop.tgz";
87 sha256 = "1a9w5k0207fysgpxx6db3a00fs5hdc2ncx99x4ccy2s0v5ndc66g";
94 config = lib.mkIf (cfg.worker.enable || cfg.master.enable) {
95 environment.systemPackages = [ cfg.package ];
98 spark-master = lib.mkIf cfg.master.enable {
99 path = with pkgs; [ procps openssh nettools ];
100 description = "spark master service.";
101 after = [ "network.target" ];
102 wantedBy = [ "multi-user.target" ];
103 restartIfChanged = cfg.master.restartIfChanged;
104 environment = cfg.master.extraEnvironment // {
105 SPARK_MASTER_HOST = cfg.master.bind;
106 SPARK_CONF_DIR = cfg.confDir;
107 SPARK_LOG_DIR = cfg.logDir;
113 WorkingDirectory = "${cfg.package}/";
114 ExecStart = "${cfg.package}/sbin/start-master.sh";
115 ExecStop = "${cfg.package}/sbin/stop-master.sh";
121 spark-worker = lib.mkIf cfg.worker.enable {
122 path = with pkgs; [ procps openssh nettools rsync ];
123 description = "spark master service.";
124 after = [ "network.target" ];
125 wantedBy = [ "multi-user.target" ];
126 restartIfChanged = cfg.worker.restartIfChanged;
127 environment = cfg.worker.extraEnvironment // {
128 SPARK_MASTER = cfg.worker.master;
129 SPARK_CONF_DIR = cfg.confDir;
130 SPARK_LOG_DIR = cfg.logDir;
131 SPARK_WORKER_DIR = cfg.worker.workDir;
136 WorkingDirectory = "${cfg.package}/";
137 ExecStart = "${cfg.package}/sbin/start-worker.sh spark://${cfg.worker.master}";
138 ExecStop = "${cfg.package}/sbin/stop-worker.sh";
146 "d '${cfg.worker.workDir}' - spark spark - -"
147 "d '${cfg.logDir}' - spark spark - -"
152 description = "spark user.";