diff --git a/meson.build b/meson.build
index e03f330f3d..f1139ecf50 100644
--- a/meson.build
+++ b/meson.build
@@ -39,6 +39,8 @@ if host_machine.system() == 'windows'
   conf.set('WINVER', '0x0600') # Win Vista / Server 2008
 endif
 
+# patched in for nix
+install_prefix = get_option('install_prefix')
 
 # set various paths
 
@@ -57,6 +59,13 @@ else
   sysconfdir = prefix / get_option('sysconfdir')
 endif
 
+# nix: don't prefix the localstatedir; some things need to write to it, so it
+# can't be in the nix store, and that's what the prefix is.
+# We'll prefix things ourselves where needed
+localstatedir = get_option('localstatedir')
+# Same for sysconfidr
+sysconfdir = get_option('sysconfdir')
+
 # if --prefix is /usr, don't use /usr/var for localstatedir or /usr/etc for
 # sysconfdir as this makes a lot of things break in testing situations
 if prefix == '/usr'
diff --git a/meson_options.txt b/meson_options.txt
index 5b43cdbd6b..e9dff18441 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -1,3 +1,5 @@
+option('install_prefix', type: 'string', value: '', description: 'prefix for nix store installation')
+
 option('no_git', type: 'boolean', value: false, description: 'Disable git submodule update')
 option('packager', type: 'string', value: '', description: 'Extra packager name')
 option('packager_version', type: 'string', value: '', description: 'Extra packager version')
diff --git a/src/libxl/meson.build b/src/libxl/meson.build
index 0cc277db82..c11c0aa97b 100644
--- a/src/libxl/meson.build
+++ b/src/libxl/meson.build
@@ -81,12 +81,12 @@ if conf.has('WITH_LIBXL')
   virt_install_dirs += [
     confdir / 'libxl',
     confdir / 'libxl' / 'autostart',
-    localstatedir / 'lib' / 'libvirt' / 'libxl',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'libxl',
     localstatedir / 'lib' / 'libvirt' / 'libxl' / 'channel',
     localstatedir / 'lib' / 'libvirt' / 'libxl' / 'channel' / 'target',
     localstatedir / 'lib' / 'libvirt' / 'libxl' / 'dump',
     localstatedir / 'lib' / 'libvirt' / 'libxl' / 'save',
-    localstatedir / 'log' / 'libvirt' / 'libxl',
-    runstatedir / 'libvirt' / 'libxl',
+    install_prefix + localstatedir / 'log' / 'libvirt' / 'libxl',
+    install_prefix + runstatedir / 'libvirt' / 'libxl',
   ]
 endif
diff --git a/src/locking/meson.build b/src/locking/meson.build
index 72f7780438..abe70d20d5 100644
--- a/src/locking/meson.build
+++ b/src/locking/meson.build
@@ -238,14 +238,14 @@ if conf.has('WITH_LIBVIRTD')
   }
 
   virt_install_dirs += [
-    localstatedir / 'lib' / 'libvirt' / 'lockd',
-    localstatedir / 'lib' / 'libvirt' / 'lockd' / 'files',
-    runstatedir / 'libvirt' / 'lockd',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'lockd',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'lockd' / 'files',
+    install_prefix + runstatedir / 'libvirt' / 'lockd',
   ]
 
   if conf.has('WITH_SANLOCK')
     virt_install_dirs += [
-      localstatedir / 'lib' / 'libvirt' / 'sanlock',
+      install_prefix + localstatedir / 'lib' / 'libvirt' / 'sanlock',
     ]
   endif
 endif
diff --git a/src/lxc/meson.build b/src/lxc/meson.build
index 99d4a34213..ee1d8445b3 100644
--- a/src/lxc/meson.build
+++ b/src/lxc/meson.build
@@ -178,8 +178,8 @@ if conf.has('WITH_LXC')
   virt_install_dirs += [
     confdir / 'lxc',
     confdir / 'lxc' / 'autostart',
-    localstatedir / 'lib' / 'libvirt' / 'lxc',
-    localstatedir / 'log' / 'libvirt' / 'lxc',
-    runstatedir / 'libvirt' / 'lxc',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'lxc',
+    install_prefix + localstatedir / 'log' / 'libvirt' / 'lxc',
+    install_prefix + runstatedir / 'libvirt' / 'lxc',
   ]
 endif
diff --git a/src/meson.build b/src/meson.build
index 88a6f15760..211a391979 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -675,7 +675,7 @@ endforeach
 
 virt_conf_files += 'libvirt.conf'
 
-install_data(virt_conf_files, install_dir: confdir)
+install_data(virt_conf_files, install_dir: install_prefix + confdir)
 install_data(virt_aug_files, install_dir: virt_aug_dir)
 
 # augeas_test_data:
@@ -738,7 +738,7 @@ foreach data : virt_daemon_confs
     output: '@0@.conf'.format(data['name']),
     configuration: daemon_conf,
     install: true,
-    install_dir: confdir,
+    install_dir: install_prefix + confdir,
   )
 
   if data.get('with_ip', false)
@@ -867,7 +867,7 @@ if conf.has('WITH_LIBVIRTD')
 
       install_data(
         init_file,
-        install_dir: sysconfdir / 'init.d',
+        install_dir: install_prefix + sysconfdir / 'init.d',
         install_mode: 'rwxr-xr-x',
         rename: [ init['name'] ],
       )
@@ -875,7 +875,7 @@ if conf.has('WITH_LIBVIRTD')
       if init.has_key('confd')
         install_data(
           init['confd'],
-          install_dir: sysconfdir / 'conf.d',
+          install_dir: install_prefix + sysconfdir / 'conf.d',
           rename: [ init['name'] ],
         )
       endif
@@ -902,10 +902,10 @@ endif
 # Install empty directories
 
 virt_install_dirs += [
-  localstatedir / 'cache' / 'libvirt',
-  localstatedir / 'lib' / 'libvirt' / 'images',
-  localstatedir / 'lib' / 'libvirt' / 'filesystems',
-  localstatedir / 'lib' / 'libvirt' / 'boot',
+  install_prefix + localstatedir / 'cache' / 'libvirt',
+  install_prefix + localstatedir / 'lib' / 'libvirt' / 'images',
+  install_prefix + localstatedir / 'lib' / 'libvirt' / 'filesystems',
+  install_prefix + localstatedir / 'lib' / 'libvirt' / 'boot',
 ]
 
 meson.add_install_script(
diff --git a/src/network/meson.build b/src/network/meson.build
index b5eff0c3ab..a0f26d624e 100644
--- a/src/network/meson.build
+++ b/src/network/meson.build
@@ -73,11 +73,11 @@ if conf.has('WITH_NETWORK')
   }
 
   virt_install_dirs += [
-    confdir / 'qemu' / 'networks',
-    confdir / 'qemu' / 'networks' / 'autostart',
-    localstatedir / 'lib' / 'libvirt' / 'network',
-    localstatedir / 'lib' / 'libvirt' / 'dnsmasq',
-    runstatedir / 'libvirt' / 'network',
+    install_prefix + confdir / 'qemu' / 'networks',
+    install_prefix + confdir / 'qemu' / 'networks' / 'autostart',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'network',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'dnsmasq',
+    install_prefix + runstatedir / 'libvirt' / 'network',
   ]
 
   configure_file(
@@ -85,12 +85,12 @@ if conf.has('WITH_NETWORK')
     output: '@BASENAME@',
     copy: true,
     install: true,
-    install_dir: confdir / 'qemu' / 'networks',
+    install_dir: install_prefix + confdir / 'qemu' / 'networks',
   )
 
   meson.add_install_script(
     meson_python_prog.path(), python3_prog.path(), meson_install_symlink_prog.path(),
-    confdir / 'qemu' / 'networks' / 'autostart',
+    install_prefix + confdir / 'qemu' / 'networks' / 'autostart',
     '../default.xml', 'default.xml',
   )
 
diff --git a/src/nwfilter/xml/meson.build b/src/nwfilter/xml/meson.build
index 0d96c54ebe..66c92a1016 100644
--- a/src/nwfilter/xml/meson.build
+++ b/src/nwfilter/xml/meson.build
@@ -25,4 +25,4 @@ nwfilter_xml_files = [
   'qemu-announce-self.xml',
 ]
 
-install_data(nwfilter_xml_files, install_dir: sysconfdir / 'libvirt' / 'nwfilter')
+install_data(nwfilter_xml_files, install_dir: install_prefix + sysconfdir / 'libvirt' / 'nwfilter')
diff --git a/src/qemu/meson.build b/src/qemu/meson.build
index 96952cc52d..3bd829d2a9 100644
--- a/src/qemu/meson.build
+++ b/src/qemu/meson.build
@@ -199,24 +199,24 @@ if conf.has('WITH_QEMU')
   endif
 
   virt_install_dirs += [
-    confdir / 'qemu',
-    confdir / 'qemu' / 'autostart',
-    localstatedir / 'cache' / 'libvirt' / 'qemu',
-    localstatedir / 'lib' / 'libvirt' / 'qemu',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'channel',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'channel' / 'target',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'checkpoint',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'dump',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'nvram',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'ram',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'save',
-    localstatedir / 'lib' / 'libvirt' / 'qemu' / 'snapshot',
-    localstatedir / 'lib' / 'libvirt' / 'swtpm',
-    localstatedir / 'log' / 'libvirt' / 'qemu',
-    localstatedir / 'log' / 'swtpm' / 'libvirt' / 'qemu',
-    runstatedir / 'libvirt' / 'qemu',
-    runstatedir / 'libvirt' / 'qemu' / 'dbus',
-    runstatedir / 'libvirt' / 'qemu' / 'slirp',
-    runstatedir / 'libvirt' / 'qemu' / 'swtpm',
+    install_prefix + confdir / 'qemu',
+    install_prefix + confdir / 'qemu' / 'autostart',
+    install_prefix + localstatedir / 'cache' / 'libvirt' / 'qemu',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'channel',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'channel' / 'target',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'checkpoint',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'dump',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'nvram',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'ram',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'save',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'qemu' / 'snapshot',
+    install_prefix + localstatedir / 'lib' / 'libvirt' / 'swtpm',
+    install_prefix + localstatedir / 'log' / 'libvirt' / 'qemu',
+    install_prefix + localstatedir / 'log' / 'swtpm' / 'libvirt' / 'qemu',
+    install_prefix + runstatedir / 'libvirt' / 'qemu',
+    install_prefix + runstatedir / 'libvirt' / 'qemu' / 'dbus',
+    install_prefix + runstatedir / 'libvirt' / 'qemu' / 'slirp',
+    install_prefix + runstatedir / 'libvirt' / 'qemu' / 'swtpm',
   ]
 endif
diff --git a/src/remote/meson.build b/src/remote/meson.build
index eb4f7a0068..b6802eeeda 100644
--- a/src/remote/meson.build
+++ b/src/remote/meson.build
@@ -235,7 +235,7 @@ if conf.has('WITH_REMOTE')
     }
 
     virt_install_dirs += [
-      localstatedir / 'log' / 'libvirt',
+      install_prefix + localstatedir / 'log' / 'libvirt',
       runstatedir / 'libvirt',
       runstatedir / 'libvirt' / 'common',
     ]
@@ -252,7 +252,7 @@ if conf.has('WITH_REMOTE')
       )
       install_data(
         log_file,
-        install_dir: sysconfdir / 'logrotate.d',
+        install_dir: install_prefix + sysconfdir / 'logrotate.d',
         rename: [ name ],
       )
     endforeach
@@ -302,7 +302,7 @@ endif
 if conf.has('WITH_SASL')
   install_data(
     'libvirtd.sasl',
-    install_dir: sysconfdir / 'sasl2',
+    install_dir: install_prefix + sysconfdir / 'sasl2',
     rename: [ 'libvirt.conf' ],
   )
 endif
diff --git a/src/security/apparmor/meson.build b/src/security/apparmor/meson.build
index 58b4024b85..feefe553f1 100644
--- a/src/security/apparmor/meson.build
+++ b/src/security/apparmor/meson.build
@@ -20,22 +20,22 @@ foreach name : apparmor_gen_profiles
     output: name,
     configuration: apparmor_gen_profiles_conf,
     install: true,
-    install_dir: apparmor_dir,
+    install_dir: install_prefix + apparmor_dir,
   )
 endforeach
 
 install_data(
   [ 'libvirt-qemu', 'libvirt-lxc' ],
-  install_dir: apparmor_dir / 'abstractions',
+  install_dir: install_prefix + apparmor_dir / 'abstractions',
 )
 
 install_data(
   [ 'TEMPLATE.qemu', 'TEMPLATE.lxc' ],
-  install_dir: apparmor_dir / 'libvirt',
+  install_dir: install_prefix + apparmor_dir / 'libvirt',
 )
 
 install_data(
   'usr.lib.libvirt.virt-aa-helper.local',
-  install_dir: apparmor_dir / 'local',
+  install_dir: install_prefix +apparmor_dir / 'local',
   rename: 'usr.lib.libvirt.virt-aa-helper',
 )
diff --git a/tools/meson.build b/tools/meson.build
index bb28a904dc..7787b08366 100644
--- a/tools/meson.build
+++ b/tools/meson.build
@@ -120,7 +120,7 @@ if conf.has('WITH_LOGIN_SHELL')
     install_rpath: libvirt_rpath,
   )
 
-  install_data('virt-login-shell.conf', install_dir: sysconfdir / 'libvirt')
+  install_data('virt-login-shell.conf', install_dir: install_prefix + sysconfdir / 'libvirt')
 endif
 
 if host_machine.system() == 'windows'
