summary refs log tree commit diff
path: root/pkgs/build-support/deterministic-uname/deterministic-uname.sh
blob: 5272bb5b3fe17799e1c07bc2aad2fe109ef6610f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#! @shell@

set -o errexit
set -o nounset

show_help() {
  @coreutils@/bin/cat << EOF
Usage: uname [OPTION]...
Print certain system information.  With no OPTION, same as -s.

  -a, --all                print all information, in the following order,
                             except omit -p and -i if unknown:
  -s, --kernel-name        print the kernel name
  -n, --nodename           print the network node hostname
  -r, --kernel-release     print the kernel release
  -v, --kernel-version     print the kernel version
  -m, --machine            print the machine hardware name
  -p, --processor          print the processor type (non-portable)
  -i, --hardware-platform  print the hardware platform (non-portable)
  -o, --operating-system   print the operating system
      --help        display this help and exit
      --version     output version information and exit
EOF
  exit 0
}

# Potential command-line options.
version=0
all=0


kernel_name=0
nodename=0
kernel_release=0
kernel_version=0
machine=0
processor=0
hardware_platform=0
operating_system=0


@getopt@/bin/getopt --test > /dev/null && rc=$? || rc=$?
if [[ $rc -ne 4 ]]; then
  # This shouldn't happen.
  echo "Warning: Enhanced getopt not supported, please open an issue in nixpkgs." >&2
else
  # Define all short and long options.
  SHORT=hvsnrvmpioa
  LONG=help,version,kernel-name,nodename,kernel-release,kernel-version,machine,processor,hardware-platform,operating-system,all

  # Parse all options.
  PARSED=`@getopt@/bin/getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@"`

  eval set -- "$PARSED"
fi

# With no OPTION, same as -s.
if [[ $# -eq 0 ]]; then
    kernel_name=1
fi

# Process each argument, and set the appropriate flag if we recognize it.
while [[ $# -ge 1 ]]; do
  case "$1" in
    --version)
      version=1
      ;;
    -s|--kernel-name)
      kernel_name=1
      ;;
    -n|--nodename)
      nodename=1
      ;;
    -r|--kernel-release)
      kernel_release=1
      ;;
    -v|--kernel-version)
      kernel_version=1
      ;;
    -m|--machine)
      machine=1
      ;;
    -p|--processor)
      processor=1
      ;;
    -i|--hardware-platform)
      hardware_platform=1
      ;;
    -o|--operating-system)
      operating_system=1
      ;;
    -a|--all)
      all=1
      ;;
    --help)
      show_help
      ;;
    --)
      shift
      break
      ;;
    *)
      echo "uname: unrecognized option '$1'"
      echo "Type 'uname --help' for a list of available options."
      exit 1
      ;;
  esac
  shift
done


KERNEL_NAME_VAL=@uSystem@
NODENAME_VAL=nixpkgs
KERNEL_RELEASE_VAL=@modDirVersion@
# #1-NixOS SMP PREEMPT_DYNAMIC Wed Dec 14 10:41:06 UTC 2022
KERNEL_VERSION_VAL="#1-NixOS Tue Jan 1 00:00:00 UTC 1980"
MACHINE_VAL=@processor@
PROCESSOR_VAL=unknown
HARDWARE_PLATFORM_VAL=unknown
OPERATING_SYSTEM_VAL=@operatingSystem@


if [[ "$version" = "1" ]]; then
    # in case some script greps for GNU coreutils.
    echo "uname (GNU coreutils) 9.1"
    echo "Nixpkgs deterministic-uname"
    exit
fi

# output of the real uname from GNU coreutils
# Darwin:
#  Darwin *nodename* 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103 arm64 arm Darwin
# NixOS:
#  Linux *nodename* 6.0.13 #1-NixOS SMP PREEMPT_DYNAMIC Wed Dec 14 10:41:06 UTC 2022 x86_64 GNU/Linux
if [[ "$all" = "1" ]]; then
    echo -n "$KERNEL_NAME_VAL $NODENAME_VAL $KERNEL_RELEASE_VAL $KERNEL_VERSION_VAL $MACHINE_VAL "
    # in help:  except omit -p and -i if unknown.
    #echo -n "$PROCESSOR_VAL $HARDWARE_PLATFORM_VAL\n"
    echo -n "$OPERATING_SYSTEM_VAL"
fi

if [[ "$kernel_name" = "1" ]]; then
    echo -n "$KERNEL_NAME_VAL"
fi

if [[ "$nodename" = "1" ]]; then
    echo -n "$NODENAME_VAL"
fi

if [[ "$kernel_release" = "1" ]]; then
    echo -n "$KERNEL_RELEASE_VAL"
fi

if [[ "$kernel_version" = "1" ]]; then
    echo -n "$KERNEL_VERSION_VAL"
fi

if [[ "$machine" = "1" ]]; then
    echo -n "$MACHINE_VAL"
fi

if [[ "$processor" = "1" ]]; then
    echo -n "$PROCESSOR_VAL"
fi

if [[ "$hardware_platform" = "1" ]]; then
    echo -n "$HARDWARE_PLATFORM_VAL"
fi

if [[ "$operating_system" = "1" ]]; then
    echo -n "$OPERATING_SYSTEM_VAL"
fi

# for newline.
echo