1diff --git a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
2index 1db9e46127..3a567e0224 100644
3--- a/nss/nss_files/files-XXX.c
4+++ b/nss/nss_files/files-XXX.c
5@@ -75,8 +75,20 @@ internal_setent (FILE **stream)
6
7 if (*stream == NULL)
8 {
9- *stream = __nss_files_fopen (DATAFILE);
10-
11+ const char *file = DATAFILE;
12+
13+ #ifdef NIX_DATAFILE
14+ // use the Nix environment variable such as `NIX_ETC_PROTOCOLS`
15+ char *path = secure_getenv (NIX_DATAFILE);
16+
17+ // if the environment variable is set, then read from the /nix/store entry instead
18+ if (path && path[0]) {
19+ file = path;
20+ }
21+ #endif
22+
23+ *stream = __nss_files_fopen (file);
24+
25 if (*stream == NULL)
26 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
27 }
28diff --git a/nss/nss_files/files-proto.c b/nss/nss_files/files-proto.c
29index c30bedc0aa..b321e68d3c 100644
30--- a/nss/nss_files/files-proto.c
31+++ b/nss/nss_files/files-proto.c
32@@ -23,6 +23,7 @@ NSS_DECLARE_MODULE_FUNCTIONS (files)
33
34 #define ENTNAME protoent
35 #define DATABASE "protocols"
36+#define NIX_DATAFILE "NIX_ETC_PROTOCOLS"
37
38 struct protoent_data {};
39
40diff --git a/nss/nss_files/files-service.c b/nss/nss_files/files-service.c
41index bfc2590699..0bff36aee5 100644
42--- a/nss/nss_files/files-service.c
43+++ b/nss/nss_files/files-service.c
44@@ -24,6 +24,7 @@ NSS_DECLARE_MODULE_FUNCTIONS (files)
45
46 #define ENTNAME servent
47 #define DATABASE "services"
48+#define NIX_DATAFILE "NIX_ETC_SERVICES"
49
50 struct servent_data {};
51