Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packaged with Nix #406

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
46 changes: 46 additions & 0 deletions build.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{ callPackage
, symlinkJoin
, fetchurl
, makeDesktopItem
, copyDesktopItems
, hl2-unwrapped ? callPackage ./hl2-unwrapped.nix { }
, hl2-wrapper ? callPackage ./hl2-wrapper.nix { inherit hl2-unwrapped; }
}:

let
name = "hl2";

icon = fetchurl {
url = "https://upload.wikimedia.org/wikipedia/commons/1/14/Half-Life_2_Logo.svg";
hash = "sha256-/4GlYVAUSZiK7eLjM/HymcGphk5s2uCPOQuB1XOstuI=";
};
in
symlinkJoin rec{
inherit name;

paths = [
hl2-unwrapped
hl2-wrapper
] ++ desktopItems;

nativeBuildInputs = [
copyDesktopItems
];

postBuild = ''
ln -s $out/bin/hl2-wrapper $out/bin/${name}

install -Dm444 ${icon} $out/share/icons/hicolor/scalable/apps/${name}.svg
'';

desktopItems = [
(makeDesktopItem {
name = "${name}";
exec = "${name}";
icon = "${name}";
desktopName = "Half-Life 2";
comment = "Built from the leaked source code";
categories = [ "Game" ];
})
];
}
111 changes: 111 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";

ivp-submodule = {
url = "github:nillerusr/source-physics";
flake = false;
};

lib-submodule = {
url = "github:nillerusr/source-engine-libs";
flake = false;
};

thirdparty-submodule = {
url = "github:nillerusr/source-thirdparty";
flake = false;
};
};

outputs = { self, nixpkgs, flake-utils, ... }@inputs:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };

# Fix for git submodules
hl2-unwrapped = (pkgs.callPackage ./hl2-unwrapped.nix { }).overrideAttrs {
postPatch = ''
rm -rf ./{ivp,lib,thirdparty}
ln -s ${inputs.ivp-submodule} ./ivp
ln -s ${inputs.lib-submodule} ./lib
ln -s ${inputs.thirdparty-submodule} ./thirdparty
'';
};
in
{
# Install the game on your own computer
packages = {
hl2 = pkgs.callPackage ./build.nix { inherit hl2-unwrapped; };
default = self.packages.${system}.hl2;
};

# Build the game for others
devShells.default = pkgs.mkShell rec {
nativeBuildInputs = with pkgs; [
makeWrapper
SDL2
freetype
fontconfig
zlib
bzip2
libjpeg
libpng
curl
openal
libopus
pkg-config
gcc
python3
];
};
}
);
}
48 changes: 48 additions & 0 deletions hl2-unwrapped.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{ stdenv
, python3
, wafHook
, callPackage
, SDL2
, freetype
, fontconfig
, zlib
, bzip2
, libjpeg
, libpng
, curl
, openal
, libopus
, pkg-config
, gcc
, extraWafConfigureFlags ? [ ]
}:

stdenv.mkDerivation {
pname = "hl2";
version = "1.0";

src = ./.;

nativeBuildInputs = [
python3
SDL2
freetype
fontconfig
zlib
bzip2
libjpeg
libpng
curl
openal
libopus
pkg-config
gcc
wafHook
];

wafConfigureFlags = [
"-T fastnative"
"--disable-warns"
"--build-flags=-Wno-error=format-security"
] ++ extraWafConfigureFlags;
}
109 changes: 109 additions & 0 deletions hl2-wrapper.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{ writeShellScriptBin
, getopt
, xorg
, hl2-unwrapped
}:

writeShellScriptBin "hl2-wrapper" ''
config_path=$HOME/.config/hl2/config
default_resource_path=$HOME/hl2

longoptions="resource-path:,impermanent,help"
shortoptions="r:h"

impermanent=false

usage() {
echo "hl2(-wrapper) usage:"
echo "Define resource_path variable in $config_path, with the argument shown below or put resource files in $default_resource_path."
grep ' \+-.*) ' $0 | sed 's/#//' | sed -r 's/([a-z])\)/\1/'
exit 0
}

# Just in case
save_newly_created_files() {
newly_created_files=$(cd $tmp_dir && find . -type f | cut -c 2-)

echo "$newly_created_files" | while read file;
do
cp $tmp_dir$file $resource_path$file
done
}

cleanup() {
if ! $impermanent; then
save_newly_created_files
fi

rm -rf $tmp_dir
}

if [[ -e $config_path ]]
then
source $config_path
fi

parsed=$(${getopt}/bin/getopt -l $longoptions -o $shortoptions -a -- "$@")

eval set -- "$parsed"

while true; do
case "$1" in
-r | --resource-path) # Defines resources folder path
resource_path="$2"
shift 2
;;
-p | --parameters) # Sets parameters passed to hl2 launcher (encapsulated with quotes)
launcher_parameters="$2"
shift 2
;;
--impermanent) # Don't save newly created files that are not in symlimked folders (you probably don't want to use this)
impermanent=true
shift 1
;;
-h | --help) # Get help
usage
exit 0
;;
--)
shift
break
;;
*)
echo "Wrong argument: $1"
usage
exit 1
;;
esac
done

if [[ ! $resource_path ]]
then
echo "The path to Half-Life 2 resource folder is not set either in a config file or with an argument. Looking for resource files in the standard directory (~/hl2)."
resource_path=$default_resource_path
fi

if [[ ! -d $resource_path ]]
then
echo "$resource_path doesn't exist. Set a proper resource path."
exit 1
elif [[ ! -d $resource_path/hl2 || ! -d $resource_path/platform ]]
then
echo "$resource_path doesn't contain 'hl2' and/or 'platform' folder. Set a proper resource path."
exit 1
fi

tmp_dir=$(mktemp -d)
echo $tmp_dir

mkdir $tmp_dir/{hl2,platform}
ln -s $resource_path/hl2/* $tmp_dir/hl2/
ln -s $resource_path/platform/* $tmp_dir/platform/
rm -rf $tmp_dir/hl2/bin

${xorg.lndir}/bin/lndir "${hl2-unwrapped}" $tmp_dir

trap cleanup EXIT

( cd $tmp_dir ; ./hl2_launcher $launcher_parameters )
''
Loading