1 diff --git a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
2 index 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)
9 - *stream = __nss_files_fopen (DATAFILE);
11 + const char *file = DATAFILE;
14 + // use the Nix environment variable such as `NIX_ETC_PROTOCOLS`
15 + char *path = secure_getenv (NIX_DATAFILE);
17 + // if the environment variable is set, then read from the /nix/store entry instead
18 + if (path && path[0]) {
23 + *stream = __nss_files_fopen (file);
26 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
28 diff --git a/nss/nss_files/files-proto.c b/nss/nss_files/files-proto.c
29 index 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)
34 #define ENTNAME protoent
35 #define DATABASE "protocols"
36 +#define NIX_DATAFILE "NIX_ETC_PROTOCOLS"
38 struct protoent_data {};
40 diff --git a/nss/nss_files/files-service.c b/nss/nss_files/files-service.c
41 index 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)
46 #define ENTNAME servent
47 #define DATABASE "services"
48 +#define NIX_DATAFILE "NIX_ETC_SERVICES"
50 struct servent_data {};