Skip to content

Instantly share code, notes, and snippets.

@grahamc
Created July 20, 2017 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamc/a68f4613d67799c7c69c7f58831e7267 to your computer and use it in GitHub Desktop.
Save grahamc/a68f4613d67799c7c69c7f58831e7267 to your computer and use it in GitHub Desktop.
#!/bin/sh
# ./nixrpm.sh /nix/store/xs7p3w2iqvv0y1d9r79wb6jrp4pm1h93-hello-2.10/bin/hello
# creates an RPM and a symlink from /usr/bin/hello to /nix/store/xs7p3w2iqvv0y1d9r79wb6jrp4pm1h93-hello-2.10/bin/hello
package="foo"
version="1.0.0"
release=1
exported_path=$1
executable=$(basename "$1")
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
function finish {
:
rm -rf "$scratch"
}
trap finish EXIT
pushd "$scratch"
export HOME="$scratch"
mkdir -p rpmbuild/SOURCES
old="nix/store"
new="_nix_rpms"
set -eux
(
cd /nix/store
tar -c $(nix-store -qR "$exported_path" | sed -e 's#^/nix/store/##') \
--strip-components 2 \
| sed -e "s#$old#$new#g" > $scratch/rpmbuild/SOURCES/archive.tar
)
cat <<EOF > foo.spec
Summary: An RPM built from the Nix path $exported_path
Name: $package
Version: $version
Release: $release
License: commercial
Source: $scratch/archive.tar
Group: Applications/System
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
%define debug_package %{nil}
%global __os_install_post /bin/true
%description
Descripted is elided because we don't know what it is, I'm just a
shell script.
%prep
%setup -qc
%build
%install
echo post-install
rm -rf \$RPM_BUILD_ROOT
mkdir -p \$RPM_BUILD_ROOT/$new
cp -r ./ \$RPM_BUILD_ROOT/$new/
mkdir -p \$RPM_BUILD_ROOT/usr/bin/
ln -s "$(echo ${exported_path} | sed -e "s#$old#$new#")" "\$RPM_BUILD_ROOT/usr/bin/${executable}"
%clean
rm -rf \$RPM_BUILD_ROOT
%files
%defattr(-,root,root)
$(nix-store -qR "$exported_path" | sed -e "s#$old#$new#")
/usr/bin/${executable}
%changelog
* Wed Feb 24 1999 Preston Brown <pbrown@redhat.com>
... shrug ...
EOF
rpmbuild -ba foo.spec
popd
set -eux
cp $scratch/rpmbuild/RPMS/x86_64/foo-*.rpm ./
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment