From f83aa6c0ea72e5957dc5e5da08653ef91184c98e Mon Sep 17 00:00:00 2001
From: "William A. Kennington III" <william@wkennington.com>
Date: Mon, 24 Nov 2014 12:39:36 -0800
Subject: [PATCH] nixos/unifi: Properly depend on mountpoints

---
 nixos/modules/services/networking/unifi.nix | 39 +++++++++++----------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/nixos/modules/services/networking/unifi.nix b/nixos/modules/services/networking/unifi.nix
index ca6738f0fe1..71dd38a3f47 100644
--- a/nixos/modules/services/networking/unifi.nix
+++ b/nixos/modules/services/networking/unifi.nix
@@ -1,9 +1,24 @@
-{ config, lib, pkgs, ... }:
+{ config, lib, pkgs, utils, ... }:
 with lib;
 let
   cfg = config.services.unifi;
   stateDir = "/var/lib/unifi";
   cmd = "@${pkgs.icedtea7_jre}/bin/java java -jar ${stateDir}/lib/ace.jar";
+  mountPoints = [
+    {
+      what = "${pkgs.unifi}/dl";
+      where = "${stateDir}/dl";
+    }
+    {
+      what = "${pkgs.unifi}/lib";
+      where = "${stateDir}/lib";
+    }
+    {
+      what = "${pkgs.mongodb}/bin";
+      where = "${stateDir}/bin";
+    }
+  ];
+  systemdMountPoints = map (m: "${utils.escapeSystemdPath m.where}.mount") mountPoints;
 in
 {
 
@@ -32,30 +47,18 @@ in
     # to be used as the working directory.
     systemd.mounts = map ({ what, where }: {
         bindsTo = [ "unifi.service" ];
-        requiredBy = [ "unifi.service" ];
-        before = [ "unifi.service" ];
+        partOf = [ "unifi.service" ];
         options = "bind";
         what = what;
         where = where;
-      }) [
-        {
-          what = "${pkgs.unifi}/dl";
-          where = "${stateDir}/dl";
-        }
-        {
-          what = "${pkgs.unifi}/lib";
-          where = "${stateDir}/lib";
-        }
-        {
-          what = "${pkgs.mongodb}/bin";
-          where = "${stateDir}/bin";
-        }
-      ];
+      }) mountPoints;
 
     systemd.services.unifi = {
       description = "UniFi controller daemon";
       wantedBy = [ "multi-user.target" ];
-      after = [ "network.target" ];
+      after = [ "network.target" ] ++ systemdMountPoints;
+      partOf = systemdMountPoints;
+      bindsTo = systemdMountPoints;
 
       preStart = ''
         # Ensure privacy of state