Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions nix-change-permissions
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

set -e

usage() {
>&2 echo "Usage: <chown|chmod> <ABSOLUTE path to file/directory under /nix to change ownership> <uid|perm> [gid]"
}

mode="$1"
path="$2"
perm_uid="$3"
gid="$4"

if [ "$mode" == "chown" ] && [ "$#" -ne 4 ]; then
usage
exit -1
elif [ "$mode" == "chmod" ] && [ "$#" -ne 3 ]; then
usage
exit -1
fi

if [[ "$(readlink -e $path)" != /nix* ]]; then
usage
exit -1
fi

if [ "$mode" == "chmod" ]; then
chmod "$perm_uid" "$path"
elif [ "$mode" == "chown" ]; then
chown -R "$perm_uid":"$gid" "$path"
else
usage
exit -1
fi

##
27 changes: 27 additions & 0 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@

#include <sqlite3.h>

int chownx(const char *__file, __uid_t __owner, __gid_t __group) {
char *envVar = std::getenv("NIX_CHANGE_PERM_BIN");
std::string changePermissionsScript =
envVar == nullptr ? "nix-change-permissions" : std::string{envVar};
std::ostringstream stringStream;
stringStream << "/bin/bash -c 'sudo " << changePermissionsScript
<< " chown " << __file << " " << __owner << " " << __group
<< "'";
return system(stringStream.str().c_str());
}

int chmodx(const char *__file, __mode_t __mode) {
if (chmod(__file, __mode) == 0) {
return 0;
}
char *envVar = std::getenv("NIX_CHANGE_PERM_BIN");
std::string changePermissionsScript =
envVar == nullptr ? "nix-change-permissions" : std::string{envVar};
std::ostringstream stringStream;
stringStream << "/bin/bash -c 'sudo " << changePermissionsScript
<< " chmod " << __file << " " << std::oct << (__mode & 0xfff)
<< "'";
return system(stringStream.str().c_str());
}

#define chmod(file, mode) chmodx(file, mode)
#define chown(file, owner, group) chownx(file, owner, group)

namespace nix {

Expand Down